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
控件內顯示爲縮略圖。 BorderBrush
和BorderThickness
屬性的值取決於從布爾屬性派生的一些條件。在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>
我在做什麼錯?
在這種情況下,您可能想要一個值或多值轉換器。 –