WaitCursor and MVVM

by Brian Keating Thu, June 03 2010 16:43

Ever wondered how to display the correct cursor in an application that is databinded to async methods?

Pretty easy solution, just databind the cursor on the window itself.

Here's how:

  • Add an IsBusy property on the DataContext (and implement INotifyPropertyChanged on it)
  • Addt the following to your window xaml

xmlns:valueConverters="clr-namespace:XXX.ValueConverters"
Cursor="{Binding IsBusy, Converter={valueConverters:CursorExtensionConverter}}" 

 

  • Create the following ValueConverter

public class CursorExtensionConverter : MarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && ((bool)value))
            return Cursors.Wait;           
        else
            return Cursors.Arrow;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return instance;
    }

    private static CursorExtensionConverter instance = new CursorExtensionConverter();
}

 

Note: Use of MarkupExtension

 

 

Tags:

Silverlight | WPF

WPF ListViewItem Commands with MVVM pattern

by Brian Keating Wed, May 12 2010 10:44

If you're interested to see how to attach commands to listview items for use with an implementation of the MVVM pattern, have a look at this.

 

<Style x:Key="Local_OpenEntityStyle"
           TargetType="{x:Type ListViewItem}">
        <Setter Property="acb:CommandBehavior.Event"
                        Value="MouseDoubleClick" />
        <Setter Property="acb:CommandBehavior.Command"
                        Value="{Binding ElementName=uiEntityListDisplay, Path=DataContext.OpenEntityCommand}" />
        <Setter Property="acb:CommandBehavior.CommandParameter"
                        Value="{Binding}" />
</Style>

 

Here the command to be fired on the MouseDoubleClick event is set, the CommandParameter, will be the data object that we click on.

Tags:

WPF | MVVM

Declarative Ria Data and Controls

by Brian Keating Wed, March 24 2010 21:27

I'm really loving this declarative approach with silverlight and wpf... (ask me why and I can't tell you ! :-)

Anyway I've just stumbled across a way of managing RiaDataContexts Declaratively
I found it on the Telerik samples.... If you've not looked at these guys controls then check them out!!

 

<navigation:Page xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"  xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
  x:Class="SiteDocs.Loler"
  xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
  xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
  xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Ria"
  xmlns:e="clr-namespace:SiteDocs.Web.Services"
  xmlns:riaData="clr-namespace:System.Windows.Data;assembly=System.Windows.Controls.Ria"
  mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 
  Style="{StaticResource PageStyle}"
>

  <Grid x:Name="LayoutRoot" >
    <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}" >

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
    
    <Grid x:Name="gridLolerLeft" >
     <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition Height="Auto"/>
     </Grid.RowDefinitions>

                    <riaControls:DomainDataSource x:Name="DomainDataSource1" AutoLoad="True" QueryName="GetLolers" PageSize="10">
                        <riaControls:DomainDataSource.DomainContext>
                            <e:LolerContext />
                        </riaControls:DomainDataSource.DomainContext>
                        <riaControls:DomainDataSource.FilterDescriptors>
                            <riaData:FilterDescriptorCollection LogicalOperator="Or" />
                        </riaControls:DomainDataSource.FilterDescriptors>
                    </riaControls:DomainDataSource>

                    <telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding Data, ElementName=DomainDataSource1}"
                             Filtering="RadGridView1_Filtering" IsBusy="{Binding IsBusy, ElementName=DomainDataSource1}" />
           <telerik:RadDataPager x:Name="RadDataPager1" Grid.Row="1" Source="{Binding Data, ElementName=DomainDataSource1}" DisplayMode="FirstLastPreviousNextNumeric, Text" IsTotalItemCountFixed="True"/>

    
    </Grid>

                </Grid>
    </ScrollViewer>
  </Grid>

</navigation:Page>

Tags:

RIA Services | Silverlight | WPF

Popups in wpf

by Brian Keating Wed, March 17 2010 17:14

Here is come code to show a popup in wpf, i've nothing in the popup at the moment, just a gradient background and border.

<Popup Name="popup1" 
   Width="{Binding ElementName=bdrCalendar, Path=ActualWidth, Converter={StaticResource MarginValueConverter}}"
   Height="150"
   Placement="Center"
   PopupAnimation="Scroll"
   AllowsTransparency="True"
   PlacementTarget="{Binding ElementName=bdrCalendar}"
   MouseDown="popup1_MouseDown" >
 <ctrls:PopupContent />
</Popup>

 

 

 

 

Tags:

WPF

Filtering data in Silverlight

by Brian Keating Sat, March 06 2010 18:23

Ever want to filter data in Silverlight? here's a simple example that uses a lambda expression to search on name (case sensitive)

 

ComboBox cbx = ((ComboBox)sender);

ICollectionView dataView =
            CollectionViewSource.GetDefaultView(this.DataContext);
if (!string.IsNullOrEmpty(cbx.Text))
    dataView.Filter = f => ((Job)f).Name.Contains(cbx.Text);
else
    dataView.Filter = null;

Tags: , ,

Silverlight | WPF

Silverlight and WPF

by Brian Keating Tue, March 02 2010 21:44

 

Tonight I tried to use the FlowDocument that you'll know well if you are familiar with WPF....

Important part above "tried to use"  .... but  ... silverlight version 3 doesn't support it :-(

Thats a second thing I've found lacking as I move some code to Silverlight, i've also found that DataTriggers don't work like the do in WPF Cry

Tags:

Silverlight | WPF

Different views in WPF/Silverlight

by Brian Keating Thu, February 25 2010 22:01

In my early WPF days I noticed the magic that having two different controls bound to the Same ObservableCollection meant that when I selected an item in one control, the same item got selected in the other.... which i I didn't want.

CollectionViewSource To the rescue

<Window.Resources>
<local:MyData x:Key="MyData"/>  
    <CollectionViewSource x:Key="AllData" Source="{StaticResource MyData}"/>  
    <CollectionViewSource x:Key="SearchData" Source="{StaticResource MyData}" Filter="MySearch"/>  
</Window.Resources> 
 
<ListBox ItemsSource="{Binding Source={StaticResource AllData}}"/>  
<ListBox ItemsSource="{Binding Source={StaticResource SearchData}}"/>

 

 

Tags:

Silverlight | WPF

WPF Textbox changing validation

by Brian Keating Tue, February 23 2010 13:42

Here's how to ensure that databinding happens when the value of a textbox changes. (as apposed to loosing forcus for example)


<TextBox Text="{Binding Interval, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />

Tags:

WPF

Checked GroupBox

by Brian Keating Wed, February 17 2010 19:47

Here's a bit of code that i'm using in an application to give this result (Checked GroupBox)

 

<GroupBox Grid.Row="3" Grid.Column="1" BorderBrush="Black" Margin="0,0,116,0">
<GroupBox.Header>
    <CheckBox x:Name="cbValidity" IsChecked="{Binding Path=HasValidity}" >Validity</CheckBox>
</GroupBox.Header>

<StackPanel>
    <StackPanel.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding ="{Binding ElementName=cbValidity, Path=IsChecked}" Value="false">
                    <Setter Property="Button.IsEnabled" Value="false"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <TextBlock Margin="10,10,0,0" Text="This job is valid from" />
    <wfi:WindowsFormsHost x:Name="propertiesCtrlHost" SnapsToDevicePixels="True" Background="Transparent" Height="22" Margin="10" Width="200" HorizontalAlignment="Left">
        <wf:DateTimePicker x:Name="dtpFrom" Format="Custom" CustomFormat="dd MMMM yyyy  HH:mm" ValueChanged="dtpFrom_ValueChanged" />
    </wfi:WindowsFormsHost>

    <TextBlock Margin="10,10,0,0" Text="This job is valid to" />
    <wfi:WindowsFormsHost SnapsToDevicePixels="True" Background="Transparent" Height="22" Margin="10" Width="200" HorizontalAlignment="Left">
        <wf:DateTimePicker x:Name="dtpTo" Format="Custom" CustomFormat="dd MMMM yyyy  HH:mm" ValueChanged="dtpTo_ValueChanged" />
    </wfi:WindowsFormsHost>
</StackPanel>
</GroupBox>

 

Tags:

WPF

WPF...Cool 3D browser

by Brian Keating Fri, January 29 2010 09:44

Pretty cool app to pass a minute or so http://chriscavanagh.wordpress.com/


Tags:

WPF