WPF - DataTemplateSelector для ListBox не работает
От: senglory  
Дата: 31.08.18 18:20
Оценка:
Вот мой 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>


Вот код ListItemTemplateSelector

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


Почему при запуске после выполнения lb = container as ListBox lb пусто? В container Listbox не приходит, а приходит ContentPresenter какой-то. Как мне ListItemTemplateSelector выбирать темплейт для Listbox?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.