我有两个 DataGridTemplateColumn 模板
<DataTemplate x:Key="firstTemplate">
<UniformGrid Grid.Column="1" Columns="2">
<Label Background="{Binding Path=Color,
Converter={StaticResource gradientBrush}}"
Content="{Binding Path=Value}"
Style="{StaticResource WhiteCellLabelStyle}"
Visibility="Visible" />
</UniformGrid>
</DataTemplate>
<DataTemplate x:Key="secondTemplate">
<UniformGrid Grid.Column="1" Columns="{Binding Converter={StaticResource getColumnsAmount}}">
<Label Background="{Binding Path=ColorData_1.Color,
Converter={StaticResource gradientBrush}}"
Content="{Binding Path=ColorData_1,
Converter={StaticResource ValueRangeConvert}}"
Style="{StaticResource WhiteCellLabelStyle}"
Visibility="{Binding Path=ColorData_1.IsSelected,
Converter={StaticResource boolConvert}}" />
<Label Background="{Binding Path=ColorData_2.Color,
Converter={StaticResource gradientBrush}}"
Content="{Binding Path=ColorData_2,
Converter={StaticResource ValueRangeConvert}}"
Style="{StaticResource WhiteCellLabelStyle}"
Visibility="{Binding Path=ColorData_2.IsSelected,
Converter={StaticResource boolConvert}}" />
<Label Background="{Binding Path=ColorData_3.Color,
Converter={StaticResource gradientBrush}}"
Content="{Binding Path=ColorData_3,
Converter={StaticResource ValueRangeConvert}}"
Style="{StaticResource WhiteCellLabelStyle}"
Visibility="{Binding Path=ColorData_3.IsSelected,
Converter={StaticResource boolConvert}}" />
</UniformGrid>
</DataTemplate>
<DataGrid Name="dgLegend"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutoGenerateColumns="False"
Background="{x:Null}"
HeadersVisibility="None"
IsHitTestVisible="True"
IsReadOnly="True"
ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn Width="Auto"
Header="exp"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Background="{Binding Path=Color>
<Label Content="{Binding Path=Color}" />
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*" Header="Range">
<DataGridTemplateColumn.CellTemplateSelector>
<local:LegendDisplayModeTemplateSelector
firstTemplate="{StaticResource firstTemplate}"
secondTemplate="{StaticResource secondTemplate}" />
</DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我的模板选择器
public class LegendDisplayModeTemplateSelector : DataTemplateSelector
{
public DataTemplate firstTemplate
{
get;
set;
}
public DataTemplate secondTemplate
{
get;
set;
}
public DisplayMode displayMode
{
get;
set;
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
TSOptions opts = (TSOptions)item;
//some other code
}
}
问题是 SelectTemplate(object item, DependencyObject container) 中的项目总是为空