我有一个类,我们将其称为 SomeClass。SomeClass 实现了 INotifyPropertyChanged ,代码如下:
public class SomeClass
{
.
.
.
private bool _isDirty;
public bool IsDirty
{
get { return this._isDirty; }
set
{
this._isDirty = value;
this.NotifyPropertyChanged("IsDirty");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我有一个使用 的实例的表单SomeClass
,称为instanceOfSomeClass
该属性全部正确触发,但主要问题是我将保存按钮绑定到该属性即。
<Button Content="Save" Height="23" Name="btnSave" IsEnabled="{Binding Path=IsDirty}" Width="60" Margin="10, 10" HorizontalAlignment="Right" Click="btnSave_Click" />
组合框 SelectionChanged 事件应该更改该属性,定义如下:
<ComboBox Name="cboListOfUsers" ItemsSource="{Binding}" SelectionChanged="cboSomeCombo_SelectionChanged"/>
(我已经删除了与问题无关的部分组合框定义,例如样式等)
关键是组合框的 DataContext 没有设置为 instanceOfSomeClass,而是自定义类的列表。
SelectionChanged 事件触发,我的代码如下所示:
instanceOfSomeClass.IsDirty = true;
instanceOfSomeClass.User = (ApplicationUser) cboSomeCombo.SelectedItem;
这会运行,尽管它确实会更改属性并引发适当的通知,但它不会启用命令按钮。我推测这是因为组合的 DataContext 与命令按钮的 DataContext 不同
我尝试在 SelectionChanged 事件中更改 DataContext ,但这只会导致组合中没有选择任何内容(虽然启用了“保存”按钮!)
任何帮助将不胜感激