在我的 WPF 应用程序中,我有一个抽象基类的 Observable 集合Test,它实际上充满了它的派生类SqlTest和ConfigTest。
我有一个组合框,允许用户从可观察集合中选择一个项目,并且根据所选测试的类型需要不同的控件。
我尝试过使用 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; }
}
Run Code Online (Sandbox Code Playgroud)
查看模型
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));
}
}
Run Code Online (Sandbox Code Playgroud)
我的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" />
Run Code Online (Sandbox Code Playgroud)
这是我在几分钟内为您编写的一个非常简单的示例。
MainWindowVm是主窗口的视图模型。它有 2 个属性,第一个是 s 列表Thing,第二个是 selected Thing。主窗口将组合框绑定到ThingswithSelectedThing作为SelectedItem. 也有一个ContentControl绑定SelctedThing,在其资源中定义了 2 DataTemplates。DataType模板的属性会自动使用,并在更改ContentControl时与其内容的类型进行比较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>
Run Code Online (Sandbox Code Playgroud)
MainWindowVm.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)));
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Thing1并且Thing2只是空课程。
| 归档时间: |
|
| 查看次数: |
2680 次 |
| 最近记录: |