有点时候,需要利用UserControl占位模板,动态替换的情况,绑定后无法获取DataContext的问题
Posted xuling-297769461
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有点时候,需要利用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 <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 }
以上是关于有点时候,需要利用UserControl占位模板,动态替换的情况,绑定后无法获取DataContext的问题的主要内容,如果未能解决你的问题,请参考以下文章
WPF:带有 2 个(或更多!)ContentPresenters 的模板或 UserControl 以在“插槽”中呈现内容