gpt4 book ai didi

wpf - DataTemplateSelector 不会被使用

转载 作者:行者123 更新时间:2023-12-04 04:55:02 25 4
gpt4 key购买 nike

在两个之间切换 ItemTemplates在我的 ListView基于当前的 Item 类型,我实现了一个 DataTemplateSelector .我是通过 Binding 添加的到 ListView .但是如果我设置了一个断点,它就永远不会被调用。我尝试了一切,没有任何效果。

public class ListViewTemplateSelector : DataTemplateSelector
{
public DataTemplate SectionTemplate { get; set; }
public DataTemplate EntryTemplate { get; set; }

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is Section)
{
return SectionTemplate;
}

if (item is Entry)
{
return EntryTemplate;
}

return base.SelectTemplate(item, container);
}
}

XAML:
<ListView DataContext="{Binding ElementName=This}" x:Name="list" Margin="200,25,0,0" ItemsSource="{Binding CurrentSelection}" ItemTemplateSelector="{StaticResource TemplateSelector}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" Width="100" />
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}" Width="300" />
</GridView>
</ListView.View>
</ListView>

<DataTemplate x:Key="sectionTemplate">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="\Icons\blue-folder.png" />
<TextBlock Text="{Binding ID}" Margin="5,0,0,0" />
</StackPanel>
</DataTemplate>

<DataTemplate x:Key="entryTemplate">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="\Icons\ui-text-field-medium.png" />
<TextBlock Text="{Binding ID}" Margin="5,0,0,0" />
</StackPanel>
</DataTemplate>

<sdk:ListViewTemplateSelector x:Key="TemplateSelector" SectionTemplate="{StaticResource sectionTemplate}" EntryTemplate="{StaticResource entryTemplate}" />

有没有人知道如何解决这个问题?

谢谢

最佳答案

来自 GridView Overview > GridView Layout and Style > 在 GridView 中定义和样式化列:

When defining the data field to display in a GridViewColumn, use the DisplayMemberBinding, CellTemplate, or CellTemplateSelector properties. The DisplayMemberBinding property takes precedence over either of the template properties.



因此,您必须删除 DisplayMemberBinding从您的 GridViewColumns 并设置 CellTemplateSelector 而不是 ListView 的 ItemTemplateSelector :
<GridView>
<GridViewColumn Header="ID" Width="100"
CellTemplateSelector="{StaticResource TemplateSelector}"/>
<GridViewColumn Header="Value" Width="300"
DisplayMemberBinding="{Binding Value}"/>
</GridView>

关于wpf - DataTemplateSelector 不会被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16875274/

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