CellTemplateSelector不会自动选择模板

时间:2011-09-21 21:23:42

标签: wpf templates datatemplate datatemplateselector

我有两个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

2 个答案:

答案 0 :(得分:4)

我找到了答案。

http://social.msdn.microsoft.com/Forums/en/wpf/thread/b47ac38a-077f-41da-99b1-8b88add693d8?prof=required

他用这种方式:

     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;
   }
}