一個簡單的辦法是綁定到內容控件的內容屬性和定義的DataTemplates爲每種類型的內容。
<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");
}
}
來源
2012-09-20 11:55:15
Per
經由切換按鈕的未檢查/經過事件的代碼改變資源 – seveves
某物像:無效ToggleButtonChecked(){myControl.Resources [ 「ControlTemplate1」] = myControl.TryFindResource( 「ControlTemplate2」); } – seveves
ohhh ...明白了......愚蠢的我。 var cc = ManagePicker.FindChild(「myControl」,0); var ctpl = ManagePicker.TryFindResource(「ControlTemplate1」)作爲ControlTemplate; cc.Template = ctpl; –
seveves