我試圖搞亂綁定到WPF的枚舉,我不喜歡它大的時間。
您可以通過將屬性映射到模型中的相應屬性來解決此問題,其中get方法中的依賴項實現爲枚舉狀態。
例如:
<Button Height="41" HorizontalAlignment="Center" Style="{StaticResource ButtonStyle}"
Margin="407,77,289,0" Name="buttonSearch" VerticalAlignment="Top" Width="137" Click="search_click"
IsEnabled="{Binding IsSearchButtonEnabled}" ...
說你有這樣的枚舉:
public enum States
{
StateOne,
StateTwo,
StateThree
}
在視圖模型,那麼你可以這樣做:
public bool IsSearchButtonEnabled
{
get
{
return ((Model.actualState) == States.StateTwo);
}
}
要自動更新,您的視圖模型必須實現INotifyPropertyChanged。我使用ViewModel總是繼承的一般實現來簡化事情。它看起來像這樣,並且應該每次調用更新視圖調用InvokePropertyChanged(string propertyName)。 ViewModel需要知道它應該更新視圖,並知道何時調用此方法。您可以使用相同的技術在模型中,並放置在模型中的事件處理程序通知在國家枚舉的二傳手變化的用戶的VM。
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static event PropertyChangedEventHandler PropertyChangedStatic;
public void InvokePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public static void InvokePropertyChangedStatic(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChangedStatic;
if (handler != null) handler(null, new PropertyChangedEventArgs(propertyName));
}
}
絕對使用我(多)ValueConverter(s),不要凌亂您的ViewModel與按鈕的可見性/啓用狀態。 –