列表框ItemTemplate选择器不选择模板

本文关键字:选择 选择器 ItemTemplate 列表 | 更新日期: 2023-09-27 18:11:03

我试图在一个网格内的列表框上使用ItemTemplateSelector,我正在创建一个不同的文件,稍后由主窗口调用。

这是我的DataTemplateSelector代码

 public class TemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;
        if (element != null && item != null && item is myClass)
        {
            myClass agg = item as myClass;
            if(agg.myType == a)
            {
                return element.FindResource("greenItemTemplate") as DataTemplate;
            }
            else if (agg.myType == b)
            {
                return element.FindResource("yellowItemTemplate") as DataTemplate;
            }
            else if (agg.myType == c)
            {
                return element.FindResource("redItemTemplate") as DataTemplate;
            }
        }
        return null;
    }
}

这里是我的xaml

 <Grid x:Class="NS.Views.ListView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:NS.Commons"
  DataContext="{Binding}">
<Grid.Resources>
    <c:myConverter x:Key="converter" />
    <c:TemplateSelector x:Key="borderSelector" />
    <DataTemplate x:Key="greenItemTemplate">
        <Border BorderThickness="3" BorderBrush="Green">
            <StackPanel HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding Path=GroupName}"/>
                <TextBlock Text="{Binding Path=myType}"/>
            </StackPanel>
        </Border>
    </DataTemplate>
           <DataTemplate x:Key="redItemTemplate">
        <Border BorderThickness="3" BorderBrush="Red">
            <StackPanel HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding Path=GroupName}"/>
                <TextBlock Text="{Binding Path=myType}"/>
            </StackPanel>
        </Border>
    </DataTemplate>
   <DataTemplate x:Key="yellowItemTemplate">
        <Border BorderThickness="3" BorderBrush="Yellow">
            <StackPanel HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding Path=GroupName}"/>
                <TextBlock Text="{Binding Path=myType}"/>
            </StackPanel>
        </Border>
    </DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding myCollectionOfMyClassObjects}" 
         Name="listBox1"
         HorizontalContentAlignment="Stretch"  
         ItemTemplateSelector="{StaticResource borderSelector}"
         >
</ListBox>

然而,尽管绑定工作良好(未格式化对象的列表出现在列表中),ItemTemplateSelector并没有调用TemplateSelector方法。我在方法的开头放了一个断点,它永远不会被调用。

有谁知道是什么问题吗?

列表框ItemTemplate选择器不选择模板

@Rachel是对的。您的DataTemplateSelector只在负载时被调用一次,而INPC的变化则不会被调用。

你所能做的就是使用ListBoxItemStyleDataTrigger切换使用的Template

类似:

<ControlTemplate x:Key="greenItemTemplate">
  <Border BorderBrush="Green"
          BorderThickness="3">
    <StackPanel HorizontalAlignment="Stretch">
      <TextBlock Text="{Binding Path=GroupName}" />
      <TextBlock Text="{Binding Path=myType}" />
    </StackPanel>
  </Border>
</ControlTemplate>
<ControlTemplate x:Key="redItemTemplate">
  <Border BorderBrush="Red"
          BorderThickness="3">
    <StackPanel HorizontalAlignment="Stretch">
      <TextBlock Text="{Binding Path=GroupName}" />
      <TextBlock Text="{Binding Path=myType}" />
    </StackPanel>
  </Border>
</ControlTemplate>
<ControlTemplate x:Key="yellowItemTemplate">
  <Border BorderBrush="Yellow"
          BorderThickness="3">
    <StackPanel HorizontalAlignment="Stretch">
      <TextBlock Text="{Binding Path=GroupName}" />
      <TextBlock Text="{Binding Path=myType}" />
    </StackPanel>
  </Border>
</ControlTemplate>
<Style x:Key="MyListBoxItemStyle"
        TargetType="{x:Type ListBoxItem}">
  <Setter Property="Template"
          Value="{DynamicResource greenItemTemplate}" />
  <Style.Triggers>
    <DataTrigger Binding="{Binding myType}"
                  Value="c">
      <Setter Property="Template"
              Value="{DynamicResource redItemTemplate}" />
    </DataTrigger>
    <DataTrigger Binding="{Binding myType}"
                  Value="b">
      <Setter Property="Template"
              Value="{DynamicResource yellowItemTemplate}" />
    </DataTrigger>
  </Style.Triggers>
</Style>

和用法:

<ListBox ItemContainerStyle="{StaticResource MyListBoxItemStyle}"
         ItemsSource="{Binding myCollectionOfMyClassObjects}" 
         Name="listBox1"
         HorizontalContentAlignment="Stretch" />