之前这篇博文WPF 做一个指示灯,用 True
和 False
两种情况来进行判断,其实也可以用 int 类型进行多个条件的判断。
XAML:
<Window.DataContext>
<local:TestVM/>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Ellipse Width="50" Height="50">
<Ellipse.Style>
<Style TargetType="Ellipse">
<Style.Triggers>
<DataTrigger Binding="{Binding PresentColor}" Value="0">
<Setter Property="Shape.Fill" Value="Orange"/>
</DataTrigger>
<DataTrigger Binding="{Binding PresentColor}" Value="1">
<Setter Property="Shape.Fill" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding PresentColor}" Value="2">
<Setter Property="Shape.Fill" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
<Button Content="换色" Command="{Binding ChangeColorCommand}" Margin="10 0 0 0" Height="30" Width="80"/>
</StackPanel>
</Grid>
ViewModel:
public class TestVM : ViewModelBase
{
private int _PresentColor = 0;
public int PresentColor
{
get { return _PresentColor; }
set { _PresentColor = value; RaisePropertyChanged(nameof(PresentColor)); }
}
private RelayCommand _changeColorCommand;
public RelayCommand ChangeColorCommand
{
get {
if (_changeColorCommand == null)
_changeColorCommand = new RelayCommand(ChangeColor);
return _changeColorCommand; }
set { _changeColorCommand = value; RaisePropertyChanged(nameof(ChangeColorCommand)); }
}
private void ChangeColor()
{
if (PresentColor < 2)
PresentColor++;
else
PresentColor = 0;
}
}
原文:https://www.cnblogs.com/huvjie/p/15265426.html