使用@ LBushKin的答案,我修改了它
public class Prop<T> : INotifyPropertyChanged
{
private T _value;
public T Value
{
get { return _value; }
set { _value = value; NotifyPropertyChanged("Value"); }
}
public event PropertyChangedEventHandler PropertyChanged;
internal void NotifyPropertyChanged(String propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
,並對其進行設置:
class MainWindow ...
// a bool with initial value of true
public static Prop<bool> optionBool { get; set; } = new Prop<bool>{ Value = true };
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// connect
DataContext = this;
}
,並使用它:
<Grid ...
<CheckBox Content="Da Check" ... IsChecked="{Binding optionBool.Value}"/>
是的,你是在正確的軌道上。你需要提出一個屬性改變的事件。谷歌INotifyPropertyChanged –
但View如何知道當某些屬性被提高某些控制需要被解決? – javapowered
該視圖將通過您擁有的綁定知道它。 – BigL