DataGridCell 内容模板选择器 Silverlight
我有一个包含动态数据(自定义 DataRow 的集合)的 DataGrid,这是我从服务器获取的。 DataRow 有一个索引器和一个属性 Data,它返回绑定的整个数据行(您将在下面看到)
我以这种方式创建 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;
我需要做什么: 我需要根据转换器返回的数据以不同的方式显示DataGridCells的内容。
我编写了模板选择器(继承 ContentControl)并将其以这种方式放入 DataGridCell 的 ContentTemplate 属性中:
<Style TargetType="sdk:DataGridCell">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<view:DataGridCellTemplateSelector Content="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
在这种情况下,我将整个 DataRow 作为选择器的内容(无法理解为什么,因为该列绑定在该行的一项)并且我的转换器没有被调用。整个数据行是默认的 DataContext,所以我猜,在这种情况下我的代码隐藏绑定只是被忽略。
因此,我尝试将模板选择器放入 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>
但在本例中,我有包含空文本的 TextBlock 作为选择器的内容(震惊)。内容更改后调用转换器。 如何创建模板选择器,它将根据我的绑定数据(调用转换器后)选择模板?
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) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
隐式数据模板看起来像这样(注意,它们是没有 x:Key 的资源):
Implicit data templates look like that (note, that they are resources without an x:Key):