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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16215158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:40:50  来源:igfitidea点击:

Listbox ItemTemplate Selector does not pick a template

c#wpfvisual-studio-2010xaml

提问by locorecto

I am trying to use an ItemTemplateSelector on a listbox within a grid that I am creating on a different file to later be called by the MainWindow.

我正在尝试在网格中的列表框上使用 ItemTemplateSelector,我在不同的文件上创建该网格,稍后由 MainWindow 调用。

Here is my DataTemplateSelector code

这是我的 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;
    }
}

Here is my xaml

这是我的 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>

However, although the binding works fine (the list of non-formatted objects appears in the list), the ItemTemplateSelector is not calling the TemplateSelector methods. I put a breakpoint at the beginning of the methods and it's never called.

但是,尽管绑定工作正常(非格式化对象的列表出现在列表中),但 ItemTemplateSelector 并未调用 TemplateSelector 方法。我在方法的开头放置了一个断点,它从未被调用过。

Does anyone knows what could be the problem?

有谁知道可能是什么问题?

采纳答案by Viv

@Rachel is right. Your DataTemplateSelectoronly gets invoked once at load pretty much and not for INPCchanges.

@Rachel 是对的。您DataTemplateSelector只在加载时被调用一次,而不是INPC更改。

What you can do for what you require is use a ListBoxItemStylewith DataTriggerswitching the Templateused

您可以根据需要做的是使用 aListBoxItemStyleDataTrigger切换Template使用的

something like:

就像是:

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

and usage:

和用法:

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