Reputation: 129
I have a DataGrid with the dynamic data (collection of custom DataRows), which i get from the server. DataRow has an indexer and a property Data which returns whole data row for the binding (you'll see below)
I create each column of a DataGrid in such a way:
DataGridTextColumn column = new DataGridTextColumn();
Binding binding = new Binding("Data[" + i.ToString() + "]");
binding.Mode = BindingMode.TwoWay;
binding.Converter = _dataContextSelector;
binding.ConverterParameter = dataColumn;
column.Binding = binding;
What I need to do: I need to display the content of the DataGridCells in different ways according to the data, which converter returns.
I wrote the template selector (which inherits ContentControl) and put it in ContentTemplate property of DataGridCell in such a way:
<Style TargetType="sdk:DataGridCell">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<view:DataGridCellTemplateSelector Content="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
In this case I have the whole DataRow as the content of my selector (can't undestand why, because the column was bound on the one item of the row) and my converter isn't called. The whole datarow is the default DataContext, so i guess, my code-behind binding is simply ignoring in this case.
So i tried to put my template selector to the ControlTemplate of the DataGridCell:
<Style TargetType="sdk:DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridCell">
<view:DataGridCellTemplateSelector Content="{TemplateBinding Content}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But in this case i have TextBlock with empty text as the content of my selector (SHOCKED). Converter is called after content was changed. How can I create the template selector, which will select the template according to the data of my binding (after converter is called) ?
Upvotes: 3
Views: 1137
Reputation: 1173
Implicit data templates look like that (note, that they are resources without an x:Key):
<UserControl.Resources>
<DataTemplate DataType="ViewModel:Contact">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding City}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
Upvotes: 0