gpt4 book ai didi

c# - Listbox ItemTemplate Selector 不选择模板

转载 作者:太空狗 更新时间:2023-10-29 17:49:31 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

@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" />

关于c# - Listbox ItemTemplate Selector 不选择模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16215158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com