【问题标题】:Multiple DataTemplates for one ListBox一个 ListBox 的多个 DataTemplates
【发布时间】:2014-10-01 06:27:54
【问题描述】:

想象一个列表框中有多种对象类型。两者都有不同的外观和呈现的信息。如果我在 ObservableCollection 中添加它们中的任何一个,它会在 ListBox 中很好地显示它们。

<DataTemplate  DataType="{x:Type local:DogData}"  >
    <Grid...
</DataTemplate>
<DataTemplate  DataType="{x:Type local:CatData}"  >
    <Grid...
</DataTemplate>

现在我希望用户能够按下按钮并切换视图以查看更详细的信息以及提供它的模板

<DataTemplate x:Key="TemplateDogDataWithImages"  >
    <Grid...
</DataTemplate>
<DataTemplate x:Key="TemplateCatDataWithImages"  >
    <Grid...
</DataTemplate>

但我只能分配一个

AnimalsListBox.ItemTemplate = this.Resources["TemplateDogDataWithImages"] as DataTemplate; 

我不想有一个并且里面有一堆触发器。

我研究过 DataTemplateSelectors http://tech.pro/tutorial/807/wpf-tutorial-how-to-use-a-datatemplateselector

有没有更好的方法?有没有办法为每种数据类型切换默认模板,这样我就可以避免 DataTemplateSelectors? 单击按钮时,如何选择 TemplateDogDataWithImages 和 TemplateCatDataWithImages 作为默认按钮,然后单击其他按钮使用 TemplateDogDataSimple 和 TemplateCatDataSimple?

【问题讨论】:

  • DataTemplateSelectors 可能是最简单的方法。
  • 您不能将DataTemplate 分配给ListBox.ItemContainerStyle 属性...线索就在其名称中。此外,ListBox.ItemContainerStyle 属性用于定义ListBoxItem,而不是数据项。也许您的意思是 ListBox.ItemTemplate 属性?
  • @Sheridan 是的,你是对的,意思是 ItemTemplate。我正在改变它

标签: c# wpf


【解决方案1】:

我认为选择器更简单(代码更少),但如果你真的想使用触发器,也许是这样的?

    namespace WpfApplication65
{
    public abstract class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string property)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }

    public abstract class DataBase : NotifyBase
    {
        bool m_showDetailed;
        public bool ShowDetailed
        {
            get { return m_showDetailed; }
            set
            {
                m_showDetailed = value;
                NotifyPropertyChanged("ShowDetailed");
            }
        }
    }

    public class DogData : DataBase { }
    public class CatData : DataBase { }

    public partial class MainWindow : Window
    {
        public List<DataBase> Items { get; private set; }

        public MainWindow()
        {
            Items = new List<DataBase>() { new DogData(), new CatData(), new DogData() };

            DataContext = this;
            InitializeComponent();
        }
    }
}

    <Window x:Class="WpfApplication65.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication65"
        Title="MainWindow"
        WindowStartupLocation="CenterScreen">

    <Window.Resources>
        <DataTemplate DataType="{x:Type l:CatData}">
            <DataTemplate.Resources>
                <DataTemplate x:Key="basic">
                    <TextBlock Text="This is the basic cat view" />
                </DataTemplate>

                <DataTemplate x:Key="detailed">
                    <TextBlock Text="This is the detailed cat view" />
                </DataTemplate>
            </DataTemplate.Resources>

            <Border BorderThickness="1"
                    BorderBrush="Red"
                    Margin="2"
                    Padding="2">
                <StackPanel>
                    <ContentPresenter Name="PART_Presenter"
                                      ContentTemplate="{StaticResource basic}" />
                    <CheckBox Content="Show Details"
                              IsChecked="{Binding ShowDetailed}" />
                </StackPanel>
            </Border>

            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding ShowDetailed}"
                             Value="True">
                    <Setter TargetName="PART_Presenter"
                            Property="ContentTemplate"
                            Value="{StaticResource detailed}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate DataType="{x:Type l:DogData}">
            <DataTemplate.Resources>
                <DataTemplate x:Key="basic">
                    <TextBlock Text="This is the basic dog view" />
                </DataTemplate>

                <DataTemplate x:Key="detailed">
                    <TextBlock Text="This is the detailed dog view" />
                </DataTemplate>
            </DataTemplate.Resources>

            <Border BorderThickness="1"
                    BorderBrush="Blue"
                    Margin="2"
                    Padding="2">
                <StackPanel>
                    <ContentPresenter Name="PART_Presenter"
                                      ContentTemplate="{StaticResource basic}" />
                    <CheckBox Content="Show Details"
                              IsChecked="{Binding ShowDetailed}" />
                </StackPanel>
            </Border>

            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding ShowDetailed}"
                             Value="True">
                    <Setter TargetName="PART_Presenter"
                            Property="ContentTemplate"
                            Value="{StaticResource detailed}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>

    <ListBox ItemsSource="{Binding Items}" />
</Window>

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多