場合によっては、UserControlプレースホルダーテンプレート、動的置換、バインド後にDataContextを取得できないという問題を使用する必要があります

場合によっては、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                  <Da​​taTrigger 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          <Da​​taTemplate x:Key = " SingleDataTemplate " >
 11              <RadioButton Content = " {Binding Name} " />
 12          </ DataTemplate>
 13          <Da​​taTemplate 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                  <Da​​taTrigger 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 = " Horizo​​ntal " >
 29                  <TextBlock Text = " 私はテンプレートです:"/>
 30                  <UserControl />
 31              </ StackPanel>
 32              <StackPanel Orientation = " Horizo​​ntal " 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(thisnew PropertyChangedEventArgs(nameof(Name)));
47              }
 48          }
 49      }
 50 }
コードを表示

 

おすすめ

転載: www.cnblogs.com/xuling-297769461/p/12720131.html