基于ViewModel属性的条件ControlTemplate

时间:2018-10-21 18:48:09

标签: c# wpf controltemplate

是否可以基于视图模型的属性“选择”另一个控件模板?

我有以下用户控制模板:

<UserControl.Template>
    <ControlTemplate TargetType="UserControl">

        <RadioButton 
            GroupName="DisplayButtons" 
            Content="{TemplateBinding Content}"/>

    </ControlTemplate>
</UserControl.Template>

基于视图模型中的布尔值,我想使用RadioButtonButton

我该如何实现?

2 个答案:

答案 0 :(得分:2)

您可以将Style与设置DataTrigger属性的Template一起使用:

<UserControl>
    <UserControl.Style>
        <Style TargetType="UserControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="UserControl">
                        <Button Content="Button" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ViewModelProperty}" Value="True">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="UserControl">
                                <RadioButton Content="RadioButton" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>
</UserControl>

答案 1 :(得分:2)

在每种情况下都可以使用不同的视图,但是在复杂模板的情况下,此方法更有用。 对于当前情况,最简单的方法是使用触发器:

方法1

<UserControl x:Class="Myusercontrolnamespace.Views.Myusercontrol" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" "  
   mc:Ignorable = "d" Height="auto" Width="auto" >

    <UserControl.Resources>
      <DataTemplate x:Key="RadioButtontTemplate">
        <RadioButton/>
      </DataTemplate>

      <DataTemplate x:Key="ButtonTemplate">
     <Button/>
      </DataTemplate>
    </UserControl.Resources>

   <Grid>
    <ContentControl Content="{Binding }">
      <ContentControl.Style>
     <Style TargetType="{x:Type ContentControl}">
        <Setter Property="ContentTemplate" Value="{StaticResource RadioButtontTemplate}" />
        <Style.Triggers>
        <DataTrigger Binding="{Binding IsRadioButton}" Value="False">
            <Setter Property="ContentTemplate" Value="{StaticResource ButtonTemplate}" />
        </DataTrigger>
        </Style.Triggers>
      </Style>
    </ContentControl.Style>
     </ContentControl>
  </Grid>
</UserControl>

方法2

<UserControl x:Class="Myusercontrolnamespace.Views.Myusercontrol" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" "  
   mc:Ignorable = "d" Height="auto" Width="auto" >

    <UserControl.Resources>
      <DataTemplate x:Key="RadioButtontTemplate">
        <RadioButton/>
      </DataTemplate>

      <DataTemplate x:Key="ButtonTemplate">
     <Button/>
      </DataTemplate>
    </UserControl.Resources>

 <Grid>
    <DataTemplate x:Key="globalControlTemplate">
     <ContentControl Content="{Binding }">
         <ContentControl.Style>
             <Style TargetType="{x:Type ContentControl}">
                 <Setter Property="ContentTemplate" Value="{StaticResource RadioButtontTemplate}" />
                 <Style.Triggers>
                     <DataTrigger Binding="{Binding ConsumerType}" Value="Business">
                        <Setter Property="ContentTemplate" Value="{StaticResource ButtonTemplate}" />
                     </DataTrigger>
                 </Style.Triggers>
             </Style>
         </ContentControl.Style>
     </ContentControl>
   </DataTemplate> 

   <ContentControl Content="{Binding globalControlTemplate}" />
 </Grid>

</UserControl>

私下