有點時候,需要利用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: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="我是模板:" /> 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" Content="多選" /> 35 </StackPanel> 36 </StackPanel> 37 </Grid> 38 </Window>
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 }