表示する必要があることを暗示し、その状態にバインドする状態に基づいて列挙型を作成します。次いで
public enum States
{
RedState,
GreenState,
OrangeState
}
ビューモデル
public class MyViewModel : INotifyPropertyChanged
{
public States CurrentState
{
get { return _CurrentState; }
set
{
_CurrentState = value;
PropertyChanged("CurrentState");
}
}
列挙楕円
をオン/オフする特定の状態を特定するパラメータにかかる変換コンストラクタがXAML
<Window.Resources>
<converters:OperationStateToVisiblity x:Key="StateToVisiblity" />
</Window.Resources>
...
<Ellipse Fill="DarkRed" Visibility="{Binding CurrentState, ConverterParameter=RedState, Converter={StaticResource StateToVisiblityReverse}}"/>
<Ellipse Fill="Green" Visibility="{Binding CurrentState, ConverterParameter=GreenState, Converter={StaticResource StateToVisiblityReverse}}" />
コンバータ
/// <summary>
/// Take the current state, passed in as value which is bound, and check it against
/// the parameter passed in. If the names match, the ellipse should be visible,
/// if not equal, then it should be collapsed.
/// </summary>
public class OperationStateToVisiblity : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value != null) &&
(parameter != null) &&
value.ToString().Equals(parameter.ToString())
? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}