简体   繁体   English

DataGridCell内容模板选择器Silverlight

[英]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) ? 如何创建模板选择器,它将根据我的绑定数据选择模板(调用转换器后)?

  • Consider using implicit data templates instead of a custom template selector. 考虑使用隐式数据模板而不是自定义模板选择器。
  • Create a custom DataGridBoundColumn and override GenerateElement. 创建自定义DataGridBoundColumn并覆盖GenerateElement。
  • In GenerateElement, return a ContentControl. 在GenerateElement中,返回ContentControl。 You have to bind the Content property of that ContentControl using the Binding property of your custom column. 您必须使用自定义列的Binding属性绑定该ContentControl的Content属性。
    • If using implicit data templates, you're done at this point. 如果使用隐式数据模板,则此时已完成。
    • If using your own DataGridCellTemplateSelector, well, just use it instead of the plain ContentControl mentioned above. 如果使用自己的DataGridCellTemplateSelector,那么只需使用它而不是上面提到的纯ContentControl。

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.

相关问题 Silverlight sdk:带有文本框的DataGridCell模板-绑定吗? - Silverlight sdk:DataGridCell Template with TextBox - the Binding? 如何降低 Silverlight 中 DataGridCell 的高度? - How to reduce the height of a DataGridCell in Silverlight? Silverlight中模板内容的动画属性 - Animating properties of the content of a template in Silverlight 从Silverlight 4中的代码背后更改DataGridCell的背景 - Change a DataGridCell's background from the codebehind in Silverlight 4 使Silverlight DataGridCell内容填充单元格? - Make Silverlight DataGridCell contents fill the cell? Silverlight模板选择器解决方案效果很好,但无法混合 - Silverlight template selector solution works great, but not blend able 在Silverlight中使文本从DataGridCell中渗出,但不从DataGrid中渗出 - Getting text to bleed out of a DataGridCell in Silverlight, but not bleed out of DataGrid 如何在代码中将Silverlight 3 DataGridCell置于编辑模式? - How can I put a Silverlight 3 DataGridCell into edit mode in code? 如何在Silverlight的内容模板中将父控件样式应用于子控件 - How to apply Style of Parent control to Child controls in content template in Silverlight Silverlight如何在绑定到复杂集合时如何将数据模板选择器用于列表框项目 - Silverlight how to use data template selector for listbox items when binding to complex collections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM