如何根据wpf(mvvm)中的bool切换边框样式

时间:2014-08-06 11:18:02

标签: c# wpf mvvm

所以这是我在xaml的边界(的一部分)

    <DataTemplate DataType="{x:Type viewModel:FunctionProviderViewModel}">
    <Border Margin="4"
            Background="PapayaWhip"
            CornerRadius="4"                
            IsEnabled="{Binding IsSimpleModeActive}"
            Style="{StaticResource ResourceKey=AcpBorder}">     

使用此DataTemplate填充Control。我添加了另一个带x:Key="SimpleAcpBorder"的边框,我在我的viewmodel中添加了一个bool(将绑定到一个复选框)。现在我想将样式设置为简单,如果选中复选框,则将其设置为常规。有人知道如何实现这个目标吗?

提前感谢!

编辑:

我的datatemplate现在看起来像这样

    <DataTemplate DataType="{x:Type viewModel:FunctionProviderViewModel}">
    <Border Background="PapayaWhip">
        <Border.Style>
            <Style TargetType="Border"                       
                   BasedOn="{StaticResource ResourceKey=AcpBorder}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSimpleModeActive}"
                                 Value="True">
                        <Setter Property="Margin"
                                Value="0" />
                        <Setter Property="CornerRadius"
                                Value="0" />                            
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>

        <Border.Effect>
            <DropShadowEffect BlurRadius="10" ShadowDepth="5" />
        </Border.Effect>
        <TreeViewItem VerticalContentAlignment="Center"
                      Background="Transparent"

在另一个xaml中,我的AcpBoarder保持不变:

    <Style x:Key="AcpBorder"
       TargetType="Border">
    <Setter Property="VerticalAlignment"
            Value="Stretch" />
    <Setter Property="CornerRadius"
            Value="3" />
    <Setter Property="Padding"
            Value="4" />
    <Setter Property="Margin"
            Value="2" />
</Style>

但不知何故,Trigger似乎对我没用。有任何想法吗?

非常感谢

EDIT2:

    System.Windows.Data Warning: 56 : Created BindingExpression (hash=54724182) for    Binding (hash=28450086)
    System.Windows.Data Warning: 58 :   Path: 'IsSimpleModeActive'
    System.Windows.Data Warning: 60 : BindingExpression (hash=54724182): Default mode resolved to OneWay
    System.Windows.Data Warning: 61 : BindingExpression (hash=54724182): Default update trigger resolved to PropertyChanged
    System.Windows.Data Warning: 62 : BindingExpression (hash=54724182): Attach to         System.Windows.Controls.Border.NoTarget (hash=22755592)
    System.Windows.Data Warning: 67 : BindingExpression (hash=54724182): Resolving source 
    System.Windows.Data Warning: 70 : BindingExpression (hash=54724182): Found data context element: Border (hash=22755592) (OK)
    System.Windows.Data Warning: 78 : BindingExpression (hash=54724182): Activate with root item FunctionProviderViewModel (hash=3473742)
    System.Windows.Data Warning: 107 : BindingExpression (hash=54724182):   At level 0 using cached accessor for FunctionProviderViewModel.IsSimpleModeActive: RuntimePropertyInfo(IsSimpleModeActive)
    System.Windows.Data Warning: 104 : BindingExpression (hash=54724182): Replace item at level 0 with FunctionProviderViewModel (hash=3473742), using accessor RuntimePropertyInfo(IsSimpleModeActive)
    System.Windows.Data Warning: 101 : BindingExpression (hash=54724182): GetValue at level 0 from FunctionProviderViewModel (hash=3473742) using RuntimePropertyInfo(IsSimpleModeActive): 'False'
    System.Windows.Data Warning: 80 : BindingExpression (hash=54724182): TransferValue - got raw value 'False'
    System.Windows.Data Warning: 89 : BindingExpression (hash=54724182): TransferValue - using final value 'False'
The thread '' (0x454) has exited with code 0 (0x0).

1 个答案:

答案 0 :(得分:4)

您可以使用DataTrigger。但是,您无法直接将DataTrigger应用于边框。相反,您必须将其应用于Style

在评论中,您似乎在许多地方使用样式AcpBorder,并且您不希望在此样式和名为IsSimpleModeActive的视图模型属性之间创建耦合。一种方法是直接设置Border.Style属性,并使样式基于AcpBorder样式:

<Border Margin="4" Background="PapayaWhip" CornerRadius="4" IsEnabled="False">
  <Border.Style>
    <Style TargetType="Border" BasedOn="{StaticResource AcpBorder}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsSimpleModeActive}" Value="True">
          <Setter Property="IsEnabled" Value="True"/>

          ... more setters

        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Border.Style>
</Border>