Binding 将UIElement绑定到本地数据
我有一个BoolValue类,我在其中声明一个bool值并将其转换为依赖属性希望我做得正确 现在在xaml中,我有一个复选框,根据bool值选择/取消选择。 请帮我查一下所有的密码Binding 将UIElement绑定到本地数据,binding,silverlight-4.0,uielement,Binding,Silverlight 4.0,Uielement,我有一个BoolValue类,我在其中声明一个bool值并将其转换为依赖属性希望我做得正确 现在在xaml中,我有一个复选框,根据bool值选择/取消选择。 请帮我查一下所有的密码 <StackPanel Height="287" HorizontalAlignment="Left" Margin="78,65,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="309" DataContext="xyz" > <C
<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>
StackPanel的实际数据上下文是什么?看起来您正在寻找属性更改,但在不同的DataContext中
提供布尔值是复选框的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}"/>
您是如何为视图设置DataContext的?只是为了记录,您并没有转换为dependency属性,而是引发PropertyChanged事件。
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"/>