[英]DataGridCell content template selector Silverlight
I have a DataGrid with the dynamic data (collection of custom DataRows), which i get from the server. 我有一个DataGrid与动态数据(自定义DataRows的集合),我从服务器获得。 DataRow has an indexer and a property Data which returns whole data row for the binding (you'll see below)
DataRow有一个索引器和一个属性Data,它返回绑定的整个数据行(你会在下面看到)
I create each column of a DataGrid in such a way: 我以这样的方式创建DataGrid的每一列:
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. 我需要做的是:我需要根据转换器返回的数据以不同的方式显示DataGridCells的内容。
I wrote the template selector (which inherits ContentControl) and put it in ContentTemplate property of DataGridCell in such a way: 我编写了模板选择器(继承了ContentControl),并以这种方式将它放在DataGridCell的ContentTemplate属性中:
<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. 在这种情况下,我将整个DataRow作为我的选择器的内容(不能解释原因,因为列绑定在行的一个项目上)并且我的转换器未被调用。 The whole datarow is the default DataContext, so i guess, my code-behind binding is simply ignoring in this case.
整个datarow是默认的DataContext,所以我想,我的代码隐藏绑定在这种情况下完全忽略了。
So i tried to put my template selector to the ControlTemplate of the DataGridCell: 所以我试图将我的模板选择器放到DataGridCell的ControlTemplate:
<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 ). 但在这种情况下,我有TextBlock,空文本作为我的选择器的内容( SHOCKED )。 Converter is called after content was changed.
内容更改后调用Converter。 How can I create the template selector, which will select the template according to the data of my binding (after converter is called) ?
如何创建模板选择器,它将根据我的绑定数据选择模板(调用转换器后)?
Implicit data templates look like that (note, that they are resources without an x:Key): 隐式数据模板看起来像那样(注意,它们是没有x:Key的资源):
<UserControl.Resources>
<DataTemplate DataType="ViewModel:Contact">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding City}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.