我有一个传统的表单布局,顶部是菜单栏,底部是状态栏 . 当用户选择菜单项时,中间的空间(表单的整个剩余客户区)被用户控件替换 - 想想可以托管多种类型文档的SDI应用程序 .
如果你知道一个更好的方法来解决这个问题,请加入 . 现在,我试图让它在一个带有ContentControl的非常简化的版本中运行,但是当它的DataContext是它时,我无法让它更新屏幕组 .
这是ViewModelA非常简单的代码 . 除了Bs之外,ViewModelB是相同的 .
namespace Dynamic_ContentControl
{
public class ViewModelA: ViewModelBase
{
public ViewModelA()
{
DisplayName = "This is A";
}
}
}
主窗口非常简单 . 它基本上声明了一个属性来保存托管控件的视图模型,并公开两个命令来分配视图模型A或B.
namespace Dynamic_ContentControl
{
public class MainViewModel: ViewModelBase
{
private ViewModelBase clientContent = null;
public ICommand ShowA { get; private set; }
public ICommand ShowB { get; private set; }
public ViewModelBase ClientContent {
get
{
return clientContent;
}
private set
{
clientContent = value;
OnPropertyChanged("ClientContent");
}
}
public MainViewModel()
{
ShowA = new RelayCommand((obj) =>
{
ClientContent = new ViewModelA();
});
ShowB = new RelayCommand((obj) =>
{
ClientContent = new ViewModelB();
});
}
}
}
最后,XAML声明一个ContentControl并将其ContentTemplate设置为名为ClientAreaTemplate的DataTemplate,其ContentPresenter指向另一个名为TextBlockLayout的DataTemplate:
<Window x:Class="Dynamic_ContentControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Dynamic_ContentControl"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="TextBlockLayout">
<TextBlock Text="{Binding Path=DisplayName}" />
</DataTemplate>
<DataTemplate x:Key="ButtonLayout">
<Button Content="{Binding Path=DisplayName}" />
</DataTemplate>
<DataTemplate x:Key="CheckBoxLayout">
<CheckBox Content="{Binding Path=DisplayName}" />
</DataTemplate>
<DataTemplate x:Key="ClientAreaTemplate">
<ContentPresenter x:Name="ContentArea" ContentTemplate="{StaticResource ResourceKey=TextBlockLayout}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=DataContext}"
Value="{x:Type vm:ViewModelB}">
<Setter TargetName="ContentArea"
Property="ContentTemplate"
Value="{StaticResource ResourceKey=ButtonLayout}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=DataContext}"
Value="{x:Type vm:ViewModelB}">
<Setter TargetName="ContentArea"
Property="ContentTemplate"
Value="{StaticResource ResourceKey=CheckBoxLayout}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<Grid>
<Button Content="Show A"
Command="{Binding Path=ShowA}"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="75" />
<Button Content="Show B"
Command="{Binding ShowB}"
HorizontalAlignment="Left"
Margin="90,10,0,0"
VerticalAlignment="Top"
Width="75" />
<Label Content="{Binding Path=ClientContent.DisplayName}"
HorizontalAlignment="Left"
Margin="170,8,0,0"
VerticalAlignment="Top" />
<ContentControl DataContext="{Binding Path=ClientContent}"
Content="{Binding}"
ContentTemplate="{StaticResource ResourceKey=ClientAreaTemplate}"
HorizontalAlignment="Left"
Margin="10,37,0,0"
VerticalAlignment="Top"
Height="198"
Width="211" />
</Grid>
</Window>
Expected behaviour
当屏幕打开时,我想要显示TextBoxLayout . 如果用户然后单击两个按钮之一,则应加载ButtonLayout或CheckBoxLayout,具体取决于分配的视图模型的实际运行时类型 .
Actual behaviour
屏幕打开时加载了TextBoxLayout,但在单击按钮时它永远不会更改为其他类型 .
我认为问题是DataTrigger尝试与类型进行比较的方式,但是在所有Output窗口中都没有绑定消息 .
1 回答
在这种情况下,您需要使用DataTemplateSelector:
Here是动态
DataTemplateSelector
的一个版本,它根据类型返回所需的DataTemplate
:Example of using