簡體   English   中英

基於ViewModel屬性的條件ControlTemplate

[英]Conditional ControlTemplate based on property of ViewModel

是否可以根據視圖模型的屬性“選擇”其他控件模板?

我有以下用戶控制模板:

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

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

    </ControlTemplate>
</UserControl.Template>

基於視圖模型中的布爾值,我想使用RadioButtonButton

我該如何實現?

您可以將StyleDataTrigger一起使用,以設置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

<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>

親切地

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM