CellTemplateSelector won't choose template automatically

左心房为你撑大大i 提交于 2019-12-08 02:56:33

问题


I have two templates for 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>

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

The problem is the item in SelectTemplate(object item, DependencyObject container) always get null


回答1:


I found the answer.

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

He used this way:

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



回答2:


I used this solution:

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


来源:https://stackoverflow.com/questions/7506839/celltemplateselector-wont-choose-template-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!