根据所选项目显示XAML元素';s类型

本文关键字:类型 元素 XAML 选项 项目 显示 | 更新日期: 2023-09-27 18:25:55

在我的WPF应用程序中,我有一个抽象基类Test的Observable集合,它实际上充满了它的派生类SqlTestConfigTest

我有一个组合框,允许用户从可观察的集合中选择一个项目,并且根据选择的测试类型想要不同的控件。

我试过使用DataTemplates,但除了列表之外,我还没能让它们正常工作。

测试.cs

public abstract class Test
{
    public string Number { get; set; } // A string as test numbers can contain multiple decimal points depending on the section
    public string Description { get; set; }
    public string Condition { get; set; }
    public string Result { get; set; }
}

查看模型

public class MainWindowViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Test> Tests { get; set; } = new ObservableCollection<Test>();
    public Test SelectedTest
    {
        get { return _selectedTest; }
        set
        {
            _selectedTest = value;
            OnPropertyChanged("SelectedTest");
        }
    }
    private Test _selectedTest;
    public MainWindowViewModel()
    {
        Tests.Add(new SqlTest("Change Me", "Check Me", "2", "A Test", "Condition", "Result"));
        Tests.Add(new ConfigTest("key", "value", "orig", "1.10", "Test2", "this is a result", "or is it?"));
        Tests.Add(new SqlTest("Change Me", "Check Me", "2", "A Test", "Condition", "Result"));
    }
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

我的xaml

<ComboBox Grid.Row="0" ItemsSource="{Binding Tests}" SelectedItem="{Binding SelectedTest}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock><Run Text="{Binding Number}" /><Run Text=" "/><Run Text="-" /><Run Text=" "/><Run Text="{Binding Description}" /></TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<!-- The Conditions of the test -->
<TextBlock Grid.Row="1" Margin="0, 5, 0, 0" Text="{Binding SelectedTest.Condition}" TextWrapping="Wrap" />
<!-- The Result of the test -->
<TextBlock Grid.Row="2" Margin="0, 5, 0, 0" Text="{Binding SelectedTest.Result}" TextWrapping="Wrap" />

根据所选项目显示XAML元素';s类型

这是我在几分钟内为您创建的一个非常简单的示例。

MainWindowVm是主窗口的视图模型。它有两个属性,第一个是Thing的列表,第二个是选定的Thing。主窗口将组合框绑定到ThingsSelectedThing作为SelectedItem。还有一个绑定到SelctedThingContentControl,在其资源中定义了2个DataTemplatesContentControl自动使用模板的DataType属性,并在SelectedThing更改时将其与内容的类型进行比较。模板中的任何标记都显示在组合框下方。

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="525"
        Height="350"
        mc:Ignorable="d">
    <Window.DataContext>
        <local:MainWindowVm />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0"
                  ItemsSource="{Binding Path=Things}"
                  SelectedItem="{Binding Path=SelectedThing}" />
        <ContentControl Grid.Row="1" Content="{Binding Path=SelectedThing}">
            <ContentControl.Resources>
                <DataTemplate DataType="{x:Type local:Thing1}">
                    <StackPanel>
                        <TextBlock Text="This Is the template for Thing1" />
                        <Button Content="This is a button!" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate DataType="{x:Type local:Thing2}">
                    <StackPanel>
                        <TextBlock Text="This Is the template for Thing2" />
                        <TextBox Text="Enter some text" />
                    </StackPanel>
                </DataTemplate>
            </ContentControl.Resources>
        </ContentControl>
    </Grid>
</Window>

主窗口Vm.cs:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication1
{
    class MainWindowVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public MainWindowVm()
        {
            Things = new ObservableCollection<object>();
            Things.Add(new Thing1());
            Things.Add(new Thing2());
        }
        public ObservableCollection<Object> Things { get; set; }
        private Object _selectedThing;
        public Object SelectedThing
        {
            get
            {
                return _selectedThing;
            }
            set
            {
                if (value != _selectedThing)
                {
                    _selectedThing = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedThing)));
                }
            }
        }
    }
}

CCD_ 16和CCD_。