WPF - 不为ListBox调用DataTemplateSelector [英] WPF - DataTemplateSelector is not called for ListBox
问题描述
这是我的XAML
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:win="clr-namespace:System.Windows;assembly=PresentationCore"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
>
<Window.Resources>
<DataTemplate x:Key="DefaultDataTemplate">
</DataTemplate>
<DataTemplate x:Key="SelectedNoteDataTemplate">
<StackPanel Background="Orange">
<TextBlock Text="Name:" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="NetworkDataTemplate">
<StackPanel>
<TextBlock Text="Name:" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="EditNetworkDataTemplate">
<StackPanel>
<TextBlock Text="BssID:" />
<TextBox Text="{Binding BssID}" />
</StackPanel>
</DataTemplate>
<local:ListItemTemplateSelector x:Key="viewInListTemplateSelector"
DefaultnDataTemplate="{StaticResource DefaultDataTemplate}"
NoteDataTemplate="{StaticResource NetworkDataTemplate}"
SelectedNoteDataTemplate="{StaticResource SelectedNoteDataTemplate}"
/>
<local:ItemTemplateSelector x:Key="editTemplateSelector"
DefaultnDataTemplate="{StaticResource DefaultDataTemplate}"
NoteDataTemplate="{StaticResource EditNetworkDataTemplate}"
/>
</Window.Resources>
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Vertical">
<ListBox
Height="300"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
VerticalContentAlignment="Top"
x:Name="RecordsList"
ItemsSource="{Binding AllRecords}"
SelectedItem="{Binding SelectedRec}"
ItemTemplateSelector="{StaticResource viewInListTemplateSelector}"
/>
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Vertical">
<ContentControl
VerticalContentAlignment="Stretch"
x:Name="TheRecord"
Content="{Binding SelectedRec}"
ContentTemplateSelector="{StaticResource ResourceKey=editTemplateSelector}">
</ContentControl>
</StackPanel>
</Grid>
</StackPanel>
</Window>
这是选择器的代码:
public class ListItemTemplateSelector : DataTemplateSelector
{
public DataTemplate DefaultnDataTemplate { get; set; }
public DataTemplate NoteDataTemplate { get; set; }
public DataTemplate SelectedNoteDataTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (null == item)
return DefaultnDataTemplate;
var lb = container as ListBox;
if (item == lb.SelectedItem)
return SelectedNoteDataTemplate;
else
return NoteDataTemplate;
}
}
一开始我调用了SelectTemplate(),但容器从不指向我的ListBox。而不是它,它指向ContentPresenter,我没想到这里。如何让SelectTemplate()为列表框选择正确的模板?
Upon start I get SelectTemplate() called, but container never points to my ListBox. Instead of it, it points at ContentPresenter which I didn't expect here. How can I make SelectTemplate() select a correct template for the Listbox?
推荐答案
Hi Senglory,
Hi Senglory,
根据根据你的描述,你可能在var lb = container中有一些问题作为ListBox,它显示ib为null,
According to your description, you may have some issue in var lb = container as ListBox, it show that the ib is null,
所以如果你想获得ListBox,你可以使用以下代码:
so if you want to get ListBox, you can use the following code:
Window window = System.Windows.Application.Current.MainWindow;
ListBox list = window.FindName("RecordsList") as ListBox;
这意味着你想要在更改选择li ListBox时更改模板,你可以看一下下面的帖子,做到这一点。
It means that you want to change Template when you change selection li ListBox, you can take a look the following thread, do this in style.
https://stackoverflow.com/questions/146269/change-wpf-datatemplate-for-listbox-item-if-selected
最好的问候,
Cherry
这篇关于WPF - 不为ListBox调用DataTemplateSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!