WPF数据绑定复选框。已选中

WPF数据绑定复选框。已选中,wpf,data-binding,checkbox,Wpf,Data Binding,Checkbox,如何将复选框的IsChecked成员绑定到表单中的成员变量 (我意识到我可以直接访问它,但我正在努力学习数据绑定和WPF) 下面是我尝试让它工作失败的例子 XAML: 注意:我在中添加了一个名称,并更改了复选框中的绑定。如果您希望ShowPending在更改时能够更新,则还需要实现ShowPending。对@will答案的补充:这就是您的DependencyProperty的外观(使用创建): #区域显示待定 /// ///ShowPending依赖项属性 /// 公共静态只读从属属性S

如何将复选框的IsChecked成员绑定到表单中的成员变量

(我意识到我可以直接访问它,但我正在努力学习数据绑定和WPF)

下面是我尝试让它工作失败的例子

XAML:



注意:我在
中添加了一个名称,并更改了复选框中的绑定。如果您希望ShowPending在更改时能够更新,则还需要实现ShowPending。

对@will答案的补充:这就是您的
DependencyProperty的外观(使用创建):

#区域显示待定
/// 
///ShowPending依赖项属性
/// 
公共静态只读从属属性ShowPendingProperty=
DependencyProperty.Register(“ShowPending”)、typeof(bool)、typeof(MainViewModel),
新的FrameworkPropertyMetadata((bool)false));
/// 
///获取或设置ShowPending属性。此依赖项属性
///表示。。。。
/// 
公共图书馆
{
获取{return(bool)GetValue(ShowPendingProperty);}
set{SetValue(ShowPendingProperty,value);}
}
#端区

您必须将绑定模式设置为双向:

<Checkbox IsChecked="{Binding Path=ShowPending, Mode=TwoWay}"/>

如果您只有一个控件要绑定到代码隐藏的属性,那么您可以通过
相对资源将其指定为绑定中的源,如下所示:

<CheckBox ...
IsChecked="{Binding ShowPending, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
完成此操作后,所有子控件的
DataContext
属性将成为
窗口
类,因此数据绑定到代码隐藏中的属性将是自然的

如果出于某种原因,您不想在窗口上设置
DataContext
,但希望将其设置在控件层次结构的较低位置,则可以使用
FindAncestor
机制来实现。例如,如果要在网格元素和网格的所有子元素上设置它:

<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
    <CheckBox ...
          IsChecked="{Binding ShowPending}">
    </CheckBox>
</Grid>

在这一点上可能值得注意的是,到目前为止,我们已经实现了将UI控件绑定到代码隐藏类的属性的能力,并且该代码隐藏属性能够随着UI元素的更改而保持最新。因此,如果用户选中复选框,
ShowPending
属性将被更新


但很多时候,你也希望反过来是真的;对源属性的更改应反映在对UI控件的相应更改中。您可以通过向窗口中添加另一个复选框控件来看到这一点,该控件绑定到相同的
ShowPending
属性。当您单击一个复选框时,您可能希望或期望另一个复选框被同步,但这不会发生。为了实现这一点,代码隐藏类应该(a)实现,(b)添加
ShowPendingChanged
事件或(c)使
ShowPending
a。在这三种机制中,我建议在代码隐藏上实现
inotifypropertychanged
,这是最常见的机制

如果属性位于
ViewModel
而不是
视图本身,您将如何进行绑定?如果使用ViewModel,您通常会将视图中(或XAML中)的DataContext设置为ViewModel,只需执行
IsChecked=“{binding ShowPending}”
,尽管许多属性的默认设置是单向,默认情况下,IsChecked属性是双向的。看见
#region ShowPending

/// <summary>
/// ShowPending Dependency Property
/// </summary>
public static readonly DependencyProperty ShowPendingProperty =
    DependencyProperty.Register("ShowPending", typeof(bool), typeof(MainViewModel),
        new FrameworkPropertyMetadata((bool)false));

/// <summary>
/// Gets or sets the ShowPending property. This dependency property 
/// indicates ....
/// </summary>
public bool ShowPending
{
    get { return (bool)GetValue(ShowPendingProperty); }
    set { SetValue(ShowPendingProperty, value); }
}

#endregion
<Checkbox IsChecked="{Binding Path=ShowPending, Mode=TwoWay}"/>
<CheckBox ...
IsChecked="{Binding ShowPending, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
<Window x:Class="MyProject.Form1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <CheckBox ... 
            IsChecked="{Binding ShowPending}">
        </CheckBox>
    </Grid>
</Window>
<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
    <CheckBox ...
          IsChecked="{Binding ShowPending}">
    </CheckBox>
</Grid>