WPF复选框绑定

WPF复选框绑定

问题描述:

虽然琐碎存储使用复选框的Click事件变量的复选框的选中状态,我将如何通过数据绑定呢?所有我发现有UI一些数据源更新或绑定一个控件到另一个例子;我想更新点击复选框,当一个成员变量。

While it is trivial to store a checkbox's checked state in a variable using the checkbox's Click event, how would I do it via databinding? All the examples I have found have the UI updated from some datasource, or bind one control to another; I want to update a member variable when the checkbox is clicked.

TIA任何指针...

TIA for any pointers...

您需要为这个依赖属性:

You need a dependency property for this:

public BindingList<User> Users
    {
        get { return (BindingList<User>)GetValue(UsersProperty); }
        set { SetValue(UsersProperty, value); }
    }

public static readonly DependencyProperty UsersProperty =
    DependencyProperty.Register("Users", typeof(BindingList<User>), 
      typeof(OptionsDialog));

一旦做到这一点,您绑定的复选框,依赖项属性:

Once that is done, you bind the checkbox to the dependency property:

<CheckBox x:Name="myCheckBox" IsChecked="{Binding ElementName=window1,
     Path=CheckBoxIsChecked}" />

有关的工作,你有你的名字在其开通标签窗口或用户控件,并在参数的ElementName使用该名称。

For that to work you have to name your Window or UserControl in its openning tag, and use that name in the ElementName parameter.

有了这个code,每当您更改code面的财产,你会改变文本框。此外,每当您检查/取消文本框,依赖项属性也会随之改变。

With this code, whenever you change the property on the code side, you will change the textbox. Also, whenever you check/uncheck the textbox, the Dependency Property will change too.

编辑:

一个简单的方法来创建一个依赖属性键入片断propdp,这将给你一般code为依赖项属性。

An easy way to create a dependency property is typing the snippet propdp, which will give you the general code for Dependency Properties.

所有code:

XAML:

<Window x:Class="*Tests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" x:Name="window1" Height="300" Width="300">
    <Grid>
     <StackPanel Orientation="Vertical">
         <CheckBox Margin="10" x:Name="myCheckBox" 
               IsChecked="{Binding ElementName=window1, Path=IsCheckBoxChecked}"
               >Bound CheckBox</CheckBox>
         <Label Content="{Binding ElementName=window1, Path=IsCheckBoxChecked}" 
               ContentStringFormat="Is checkbox checked? {0}"></Label>
      </StackPanel>     
    </Grid>
</Window>

C#:

using System.Windows;

namespace *Tests
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public bool IsCheckBoxChecked
        {
           get { return (bool)GetValue(IsCheckBoxCheckedProperty); }
           set { SetValue(IsCheckBoxCheckedProperty, value); }
        }

       // Using a DependencyProperty as the backing store for 
         //IsCheckBoxChecked.  This enables animation, styling, binding, etc...
       public static readonly DependencyProperty IsCheckBoxCheckedProperty =
           DependencyProperty.Register("IsCheckBoxChecked", typeof(bool), 
             typeof(Window1), new UIPropertyMetadata(false));

      public Window1()
      {             
            InitializeComponent();
      }
    }
}

注意如何仅code的背后,是依赖属性。无论是标签和复选框绑定到它。如果该复选框改变,标签也发生了变化。

Notice how the only code behind is the Dependency Property. Both the label and the checkbox are bound to it. If the checkbox changes, the label changes too.