我有两个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>
我的TemplateSelector
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(对象项,DependencyObject容器)中的项始终为null
答案 0 :(得分:4)
我找到了答案。
他用这种方式:
class UserCellEdit : DataTemplateSelector
{
public override DataTemplate
SelectTemplate(object item, DependencyObject container)
{
ContentPresenter presenter = container as ContentPresenter;
DataGridCell cell = presenter.Parent as DataGridCell;
Guideline_node node = (cell.DataContext as Guideline_node);
//,...... etc. the rest of the code
}
}
答案 1 :(得分:1)
我使用了这个解决方案:
public class EmployeeListDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement element = container as FrameworkElement;
if (element != null && item != null && item is Employee)
{
Employee employee = item as Employee;
if (employee.Place == "UK")
return element.FindResource("UKdatatemp") as DataTemplate;
else
return element.FindResource("Otherdatatemp") as DataTemplate;
}
return null;
}
}