State.AwayとState.Stopでボタンを表示したいのですが、State.AwayとState.Stopと異なる場合でも何らかの理由でボタンが常に表示されます。XAMLのボタンの可視性をビューモデルにバインドしますか?
XAML:
<Button Text="Hello" IsVisible="{Binding View}"/>
のViewModel:あなたは次にあなたがコンバータにあなたのボタンをバインドVisibility
public class StateToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value is State)
{
State state = (State)value;
switch (state)
{
case State.Away:
case State.Gone:
return Visibility.Visible;
default:
return Visibility.Collapsed;
}
}
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return State.None; // your default state
}
}
へState
からIValueConverter
を作成することができます
private bool myBool;
public bool View
{
get
{
if (State == State.Away || State == State.Gone)
{
myBool = true;
}
else
{
myBool = false;
}
return myBool;
}
}
VMはINotifyPropertyChangedを実装し、Viewの値が変更されたときにPropertyChangedイベントを発生させる必要があります – Jason
常にfalseを返すとボタンが見えませんか?次に、バインディングは、「状態」が変更されたときに「ビュー」が変更されたことをおそらく認識しません。 –
また、コントロールのDataContextがViewModelに正しく設定されていることを確認してください。 – Viju