2014-03-05 57 views
0

我的WPF應用程序有基於兩個不同的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; } 
    } 

    // . . . 
} 

我在顯示這些MyViewModel窗口的ListBox對象。該對象的數據包含一個圖像,該圖像在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在XAML爲在ListBox使用的DataTemplate

<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

視圖模型應該是視圖的模型。因此,請爲您提供正確的視圖模型。我的意思是保持視圖模型中的複雜邏輯並將其作爲簡單的bool屬性公開。

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

然後你可以使用一個簡單的DataTrigger每個組合條件,在底部的最重要的條件,命令他們,因爲那些宣降將覆蓋那些宣佈在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。無論採用哪種方式,最好儘可能簡化界面,並將複雜的功能保留在視圖模型中。