[英]WPF Changing ContentControl Template via Button from another control
我具有以下控件/视图:
UserControl.Resources
包含一个ContentControl和两个ControlTemplates 我想通过主应用程序中的ToggleButton在两个ControlTemplates之间进行切换。
这应该很容易...但是我找不到合适的方法:S
一个更简单的解决方案是绑定到内容控件的content属性,并为每种类型的内容定义数据模板。
<Window.Resources>
<DataTemplate DataType="{x:Type local:MyType1}">
<Border Background="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:MyType2}">
<Border Background="Green" />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="24" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding MyContent}" />
<ToggleButton Grid.Row="1"
Content="Toggle"
IsChecked="{Binding IsChecked}" />
</Grid>
//DataContext
public bool IsChecked
{
get { return isChecked_; }
set
{
isChecked_ = value;
NotifyPropertyChanged_("IsChecked");
if (value)
MyContent = new MyType1();
else
MyContent = new MyType2();
}
}
public object MyContent
{
get { return myContent_; }
set
{
myContent = value;
NotifyPropertyChange_("MyContent");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.