場合によっては、UserControlプレースホルダーテンプレート、動的置換、バインド後にDataContextを取得できないという問題を使用する必要があります。
効果は次のとおりです。
重要な点は、以下の3行目で、現在のコンテキストをContentに渡し、バインドされたContentTemplateを生成して、UserControlにバインドされたDataContextを取得する必要があることです。
1 <Style TargetType = " UserControl " > 2 <Setter Property = " ContentTemplate " Value = " {StaticResource SingleDataTemplate} " /> 3 <Setter Property = " Content " Value = " {Binding} " /> 4 <Style.Triggers> 5 <DataTrigger Binding = " {Binding ElementName = rabtn、Path = IsChecked} " Value = " false "> 6 <Setter Property = " ContentTemplate " Value = " {StaticResource MultipleTemplate} " /> 7 </ DataTrigger> 8 </Style.Triggers> 9 </ Style>
完成したフロントエンドとバックエンドのコードは次のとおりです。
1 <Window x:Class = " VideoAndAudioDemo.MainWindow " 2 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation " 3 xmlns:x = " http://schemas.microsoft.com/ winfx / 2006 / xaml " 4 xmlns:d = " http://schemas.microsoft.com/expression/blend/2008 " 5 xmlns:mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 " 6 xmlns:local = " clr-namespace:VideoAndAudioDemo " 7 mc:無視できる= "d " 8 Title = " MainWindow " Height = " 450 " Width = " 800 " > 9 <Window.Resources> 10 <DataTemplate x:Key = " SingleDataTemplate " > 11 <RadioButton Content = " {Binding Name} " /> 12 </ DataTemplate> 13 <DataTemplate x:Key = " MultipleTemplate " > 14 <CheckBox Content = "{Binding Name} " /> 15 </ DataTemplate> 16 <Style TargetType = " UserControl " > 17 <Setter Property = " ContentTemplate " Value = " {StaticResource SingleDataTemplate} " /> 18 <Setter Property = " Content " Value = " {Binding} " /> 19 <Style.Triggers> 20 <DataTrigger Binding = "{Binding ElementName = rabtn、Path = IsChecked} 」Value = " false " > 21 <Setter Property = " ContentTemplate " Value = " {StaticResource MultipleTemplate} " /> 22 </ DataTrigger> 23 </Style.Triggers> 24 </ Style> 25 </Window.Resources> 26 <グリッド> 27 <StackPanel> 28 <StackPanel Orientation = " Horizontal " > 29 <TextBlock Text = " 私はテンプレートです:"/> 30 <UserControl /> 31 </ StackPanel> 32 <StackPanel Orientation = " Horizontal " Margin = " 0 20 0 0 " > 33 <RadioButton x:Name = " rabtn " IsChecked = " True " GroupName = " selected " Content = " 单选" /> 34 <RadioButton GroupName = " selected "コンテンツ= 「複数選択」/> 35 </ StackPanel> 36 </ StackPanel> 37 </ Grid> 38 </ Window>
1 システムを使用。 2 System.Collections.Generic を使用します。 3 System.ComponentModel を使用します。 4 System.Linq を使用します。 5 System.Media を使用。 6 System.Text を使用。 7 System.Threading.Tasks を使用します。 8 System.Windows を使用。 9 System.Windows.Controls を使用します。 10 System.Windows.Data を使用します。 11 System.Windows.Documents を使用します。 12 System.Windows.Input を使用します。 13 使用System.Windows.Media; 14 System.Windows.Media.Imagingの使用; 15 System.Windows.Navigation を使用します。 16 System.Windows.Shapes を使用; 17 18 名前空間VideoAndAudioDemo 19 { 20 /// <summary> 21 /// MainWindow.xaml的交互逻辑 22 /// </ summary> 23 パブリック 部分 クラスMainWindow:Window 24 { 25 public MainWindow() 26 { 27 InitializeComponent( ); 28 var vm = new MainWindowViewModel(); 29 DataContext = vm; 30 31 vm.Name = " 测试" ; 32 } 33 } 34 35 パブリック クラスMainWindowViewModel:INotifyPropertyChanged 36 { 37 パブリック イベントPropertyChangedEventHandler PropertyChanged; 38 プライベート 文字列名。 39 40 パブリック ストリングName 41 { 42 get => 名前; 43 セット 44 { 45 名前= 値; 46 PropertyChanged?.Invoke(this、new PropertyChangedEventArgs(nameof(Name))); 47 } 48 } 49 } 50 }