如何更改ComboBox项时使用ContentPresenter更改内容?更改项目时-未选择模板,但在“窗口加载”模板上选择了正确的模板。如何在下面修复我的示例?
我尝试设置 Content =“ {Binding SelectedDocTypeID}” 并将组合框项目更改为调用方法SelectTemplate,但是为什么同一个MyModelObject属性不能这样做呢?如果我不使用这种方法,那么在DataTemplate中,我需要像这样重新绑定: MyModelObject .Name等(并稍微改变一下DataTemplateSelector)。但是我不想绑定到DataTemplate中的对象名称。
我的模型和组合框数据类是:
public class MyData
{
public string Name { get; set; }
public string SubName { get; set; }
public int? DocTypeID { get; set; }
// etc
}
public class MyDic
{
public int ID { get; set; }
public string Name { get; set; }
}
我的视图模型是:
public class MyViewModel : INotifyPropertyChanged
{
public List<MyDic> MyDicList { get; set; }
public MyData MyModelObject { get; set; }
public int? SelectedDocTypeID
{
get { return MyModelObject.DocTypeID; }
set
{
MyModelObject.DocTypeID = value;
OnPropertyChanged();
OnPropertyChanged("MyModelObject");
}
}
public MyViewModel()
{
MyDicList = new List<MyDic>()
{
new MyDic() { ID = 1, Name = "First" },
new MyDic() { ID = 2, Name = "Second" },
new MyDic() { ID = 3, Name = "Third" },
new MyDic() { ID = 4, Name = "Fourth" },
new MyDic() { ID = 5, Name = "Fifth" }
};
MyModelObject = new MyData() { Name = "First Name", SubName = "Sub Name", DocTypeID = 1 };
}
}
我的 XAML 是:
<Window x:Class="TestContentPresenter.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestContentPresenter"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:MyTemplateSelector x:Key="MyTemplateSelector"/>
<DataTemplate x:Key="DefaultTemplate">
<StackPanel Margin="5">
<TextBlock Text="Empty"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="FirstTemplate">
<StackPanel Margin="10">
<TextBox Text="{Binding Name}" FontSize="14" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="SecondTemplate">
<StackPanel Margin="20">
<TextBox Text="{Binding SubName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<ComboBox ItemsSource="{Binding MyDicList}" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValue="{Binding SelectedDocTypeID}" Margin="10"/>
<ContentPresenter Content="{Binding MyModelObject}" ContentTemplateSelector="{StaticResource MyTemplateSelector}" Margin="10"/>
</StackPanel>
</Grid>
</Window>
我的 DataTemplateSelector 是:
public class MyTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var defaultTemplate = (container as FrameworkElement).FindResource("DefaultTemplate") as DataTemplate;
if (item == null || !(item is MyData))
{
return defaultTemplate;
}
switch ((int?)(item as MyData).DocTypeID)
{
case 1:
return (container as FrameworkElement).FindResource("FirstTemplate") as DataTemplate;
case 2:
return (container as FrameworkElement).FindResource("SecondTemplate") as DataTemplate;
}
return defaultTemplate;
}
}