简体   繁体   中英

DataGridCell content template selector Silverlight

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

  • Consider using implicit data templates instead of a custom template selector.
  • Create a custom DataGridBoundColumn and override GenerateElement.
  • In GenerateElement, return a ContentControl. You have to bind the Content property of that ContentControl using the Binding property of your custom column.
    • 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM