Sometimes, you need to use the UserControl placeholder template, dynamic replacement, the problem that the DataContext cannot be obtained after binding

Sometimes, you need to use the UserControl placeholder template, dynamic replacement, the problem that the DataContext cannot be obtained after binding, hereby remarks

The effect is as follows:

The key point is that in the third line below, you need to pass the current context to the Content and generate the bound ContentTemplate to get the DataContext bound to the UserControl

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>

Here is the completed front-end and back-end code:

 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:Ignorable="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     <Grid>
27         <StackPanel>
28             <StackPanel Orientation="Horizontal">
29                 <TextBlock Text = "I am a template:" />
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""Multiple Choice"Content = />
35             </StackPanel>
36         </StackPanel>
37     </Grid>
38 </Window>
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Linq;
 5 using System.Media;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using System.Windows;
 9 using System.Windows.Controls;
10 using System.Windows.Data;
11 using System.Windows.Documents;
12 using System.Windows.Input;
13 using System.Windows.Media;
14 using System.Windows.Media.Imaging;
15 using System.Windows.Navigation;
16 using System.Windows.Shapes;
17 
18 namespace VideoAndAudioDemo
19 {
20     /// <summary>
21     /// MainWindow.xaml 的交互逻辑
22     /// </summary>
23     public partial class 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     public class MainWindowViewModel : INotifyPropertyChanged
36     {
37         public event PropertyChangedEventHandler PropertyChanged;
38         private string name;
39 
40         public string Name
41         {
42             get => name;
43             set
44             {
45                 name = value;
46                 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
47             }
48         }
49     }
50 }
View Code

 

Guess you like

Origin www.cnblogs.com/xuling-297769461/p/12720131.html