Saturday, December 8, 2012

Binding to Property and ItemSource with DataGrid

Binding to Property and ItemSource with DataGrid

I have a DataGrid that's currently has an ItemsSource="{Binding Events}", in one of the columns I need to bind to a property (OccurredOnLocal) that's not part of ItemsSource="{Binding Events}". How would I do this?

EventHistoryView.xaml

<sdk:DataGrid x:Name="dataGrid" Grid.Row="3" Margin="5,5" ItemsSource="{Binding Events}"  IsReadOnly="True" AutoGenerateColumns="False" IsEnabled="{Binding DataIsCurrent}">         <i:Interaction.Behaviors>             <behaviors:DataGridDoubleClickBehavior DoubleClickCommand="{Binding DoubleClickCommand}" CommandParameter="{Binding SelectedItem, ElementName=dataGrid}"/>         </i:Interaction.Behaviors>         <sdk:DataGrid.Columns>             <sdk:DataGridTextColumn Header="Priority" Binding="{Binding Priority}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="auto"/>             <sdk:DataGridCheckBoxColumn Header="Requires Ack" Binding="{Binding RequiresAcknowledgement}" Width="auto"/>             <sdk:DataGridTextColumn Header="Occurred On" Binding="{Binding MessageTime}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>             <sdk:DataGridTextColumn Header="Occurred On (Local)" Binding="{Binding OccurredOnLocal}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>             <sdk:DataGridTemplateColumn Header="Alarm" Width="*">                 <sdk:DataGridTemplateColumn.CellTemplate>                     <DataTemplate>                         <Border Background="{Binding DisplayColour}">                             <TextBlock Text="{Binding EventDisplayText}" VerticalAlignment="Center"/>                         </Border>                     </DataTemplate>                 </sdk:DataGridTemplateColumn.CellTemplate>             </sdk:DataGridTemplateColumn>             <sdk:DataGridTextColumn Header="Controller" Binding="{Binding ControllerDisplayText}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>             <sdk:DataGridTextColumn Header="Reader" Binding="{Binding ReaderDisplayText}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>             <sdk:DataGridTextColumn Header="Card Number" Binding="{Binding BadgeId}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="auto"/>             <sdk:DataGridTextColumn Header="Person" Binding="{Binding PersonDisplayText}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="*"/>         </sdk:DataGrid.Columns>     </sdk:DataGrid> 

EventHistoryViewModel.cs

    #region OccurredOnLocal Property     /// <summary>     /// The <see cref="OccurredOnLocal" /> property's name.     /// </summary>     public const string OccurredOnLocalPropertyName = "OccurredOnLocal";     private TimeZoneInfo occurredOnLocal = TimeZoneInfo.Local;      /// <summary>     /// Gets the OccurredOnLocal property.     /// TODO Update documentation:     /// Changes to that property's value raise the PropertyChanged event.      /// This property's value is broadcasted by the Messenger's default instance when it changes.     /// </summary>     public TimeZoneInfo OccurredOnLocal     {         get         {             return occurredOnLocal;         }          set         {             if (occurredOnLocal == value)             {                 return;             }              occurredOnLocal = value;              RaisePropertyChanged(OccurredOnLocalPropertyName);         }     }     

No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog