需要帮助根据几个条件创建样式

时间:2014-03-05 21:44:45

标签: c# wpf

我的WPF应用程序有一个View Model对象,它有两个基于两个不同int属性的布尔属性:

public class MyViewModel : ModelBase {

    public int Class { 
        get { return iClass; }
        set {
            iClass = value;
            OnPropertyChanged( "Class" );
            OnPropertyChanged( "IsClass1" );
        }
    }
    private int iClass;

    public int Status { 
        get { return iStatus; }
        set {
            iStatus = value;
            OnPropertyChanged( "Status" );
            OnPropertyChanged( "IsStatus1" );
            OnPropertyChanged( "IsStatus2" );
            OnPropertyChanged( "IsStatus3" );
        }
    }
    private int iStatus;

    public bool IsClass1 {
        get { return Class == Class1; }
    }

    public bool IsStatus1 {
        get { return Status == Status1; }
    }

    public bool IsStatus2 {
        get { return Status == Status2; }
    }

    public bool IsStatus3 {
        get { return Status == Status3; }
    }

    // . . .
}

我在窗口中显示ListBox,显示这些MyViewModel个对象。对象的数据包含一个图像,该图像在Border控件内显示为缩略图。 BorderBrushBorderThickness属性'值取决于从布尔属性派生的某些条件。在C#中,这些条件的代码如下所示:

string color = alarm.IsClass1  ? "BorderColor1" :
               alarm.IsStatus1 ? "BorderColor2" :
               alarm.IsStatus2 ? "BorderColor3" :
               alarm.IsStatus3 ? "BorderColor4" :
               "BorderColor5";
Border.SetResourceReference( BorderBrushProperty, color );
Border.BorderThickness = new Thickness( alarm.IsStatus1 || alarm.IsStatus3 ? 4.0 : 2.0 );

我想在Style中使用DataTemplate的XAML中将其放入ListBox

<DataTempate DataType="{x:Type local:MyViewModel}">
    <Border BorderBrush="Black"
            BorderThickness="2"
            HorizontalAlignment="Center" 
            Margin="5" 
            Height="100"
            Name="Border" 
            VerticalAlignment="Center" 
            Width="100">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Image Grid.Row="0" 
                   Name="AlarmImage" 
                   Source="{Binding Path=Image}" 
                   Stretch="Fill" />
            <local:ResponseTimer Expired="Timer_Expired"
                              Grid.Row="1"
                              HideIfStatus1="True"
                              IsTabStop="False"
                              MinHeight="10"
                              x:Name="TheTimer"
                              TimeoutPeriod="00:02:30"
                              VerticalAlignment="Bottom" />
        </Grid>
    </Border>
</DataTemplate>

我尝试了以下操作,但它不起作用:

<Style TargetType="Border">
    <Setter Property="BorderBrush" Value="{DynamicResource BorderColor5}" />
    <Setter Property="BorderThickness" Value="2" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsStatus1}" Value="true">
            <Setter Property="BorderThickness" Value="4" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsStatus3}" Value="true">
            <Setter Property="BorderThickness" Value="4" />
        </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsClass1}" Value="True">
            <Setter Property="BorderBrush" Value="{DynamicResource BorderColor1}" />
        </DataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Path=IsClass1}" Value="False" />
                <Condition Binding="{Binding Path=IsStatus1}" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderBrush" Value="{DynamicResource BorderColor2}" />
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Path=IsClass1}" Value="False" />
                <Condition Binding="{Binding Path=IsStatus2}" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderBrush" Value="{DynamicResource BorderColor3}" />
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Path=IsClass1}" Value="False"/>
                <Condition Binding="{Binding Path=IsStatus3}" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter Property="BorderBrush" Value="{DynamicResource BorderColor4}" />
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

视图模型应该是视图的模型。因此,请为您提供所需视图的正确模型。我的意思是在视图模型中保留复杂的逻辑并将其公开为简单的bool属性。

public bool IsCombinedCondition1 { get; set; } // Implement INotifyPropertyChanged
...
public bool IsCombinedConditionN { get; set; } // Implement INotifyPropertyChanged

然后你可以为每个组合条件使用一个简单的DataTrigger,在底部对最重要的条件进行排序,因为声明为lower的那些将覆盖在XAML中声明更高的条件:

<DataTrigger Binding="{Binding Path=IsCombinedCondition1}" Value="true">
    <Setter Property="BorderBrush" Value="{DynamicResource BorderColor1}" />
</DataTrigger>
...
<DataTrigger Binding="{Binding Path=IsCombinedConditionN}" Value="true">
    <Setter Property="BorderBrush" Value="{DynamicResource BorderColorN}" />
</DataTrigger>

您也可以使用enum。无论哪种方式,最好保持UI尽可能简单,并在视图模型中保留复杂的功能。