我正在使用一个类“BoolValue”,其中我声明一个bool值并将其转换为Dependency Property(希望我做的正确) 现在在xaml中我有一个复选框想根据bool值检查/取消选中。 我附上整个代码人员,请帮助。
<StackPanel Height="287" HorizontalAlignment="Left" Margin="78,65,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="309" DataContext="xyz" >
<CheckBox Content="" Height="71" Name="checkBox1" IsChecked="{Binding Path=IsCkecked, Mode=TwoWay}"/>
</StackPanel>
这是班级
public class BoolValue : INotifyPropertyChanged
{
private bool _isCkecked;
public bool IsCkecked
{
get { return _isCkecked; }
set
{
if (value == _isCkecked)
return;
_isCkecked = value;
RaisePropertyChanged("IsCkecked");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string property)
{
PropertyChangedEventArgs args = new PropertyChangedEventArgs(property);
var handler = this.PropertyChanged;
//handler(this, args);
if (handler != null)
{
handler(this, args);
}
}
}
答案 0 :(得分:0)
DataContext
的实际StackPanel
是多少?看起来你正在寻找属性变化但是在DataContext
不同。
提供BoolValue
是您的CheckBox的DataContext
,下面应该有效:
public class BoolValue : INotifyPropertyChanged
{
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
if (isChecked != value)
{
isChecked = value;
NotifyPropertyChanged("IsChecked");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String propertyName)
{
// take a copy to prevent thread issues
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML:
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"/>