68484

Binding to Property and ItemSource with DataGrid

Question:

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?

<strong>EventHistoryView.xaml</strong>

<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>

<strong>EventHistoryViewModel.cs</strong>

#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); } } #endregion

Thanks,<br /> Jason

Answer1:

You can use RelativeSource xaml extension

<sdk:DataGridTextColumn Header="Occurred On (Local)" Binding="{Binding DataContext.OccurredOnLocal, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>

Recommend

  • DataTrigger in CellTemplate binding to HeaderTemplate; can it work?
  • Design Time null reference error
  • DataGrid not scrolling smoothly
  • Datagrid and column count
  • WPF DataGrid shows dot instead comma
  • WPF Binding a MenuItem in a CompositeCollection not working
  • C#/WPF: Binding to an element outside of the visual/logical tree
  • iOS/Swift: in which function between viewDidLoad and viewWillAppear am I supposed to query a databas
  • Not able to stretch an inner StackPanel
  • How to make text wrap in a WPF TreeViewItem?
  • Why java statement evaluation is happening like these ?
  • How to use a MultiBinding on DataGridTextColumn?
  • How does Skobbler prioritize which POIs to show?
  • Binding a list with another list with WPF
  • Finding number of samples in a .wav header
  • css active on html inside php echo doesn't stay when click is left
  • Stretch a span across a td
  • Webpack-dev-server and isomorphic react-node application
  • Different Datacontext for Command and CommandParameter
  • angular2 http.post() to local json file
  • undefined reference error due to use of static variables [duplicate]
  • Cannot style mat-tab without ::ng-deep and !important
  • richtextbox to string
  • Best HTML5 structure for a layout where the title/header is outside the article tag
  • Log4j cannot find properties file in a simple application
  • How to align an image side by side with a heading element?
  • Formatting a text in a table cell with PHPWord e.g. bold, font, size e.t.c
  • How to use animated gif in Firemonkey?
  • Creating a C++ function that calls other Lua function
  • incomplete type 'struct' error in C
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • WPF version of .ScaleControl?
  • Primefaces :radioButton inside a ui:repeat
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • saving file generated by TCPDF
  • How to view images from protected folder with php?
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • Textfile Structure (tables)
  • vba code to select only visible cells in specific column except heading