【问题标题】:Programmatically create a checkbox binding for a WPF form以编程方式为 WPF 表单创建复选框绑定
【发布时间】:2016-04-07 20:51:21
【问题描述】:

我需要以编程方式为 WPF 表单中的复选框创建绑定。因为复选框位于多次添加到表单的用户控件中,所以我不确定如何执行此操作。我已经为 DevExpress RichEdit 控件创建了一个绑定,该控件可以工作,然后修改复选框的代码,但它没有工作。

我返回绑定的代码如下:

private Binding SetIsCorrectBinding(int row) 
    {
        Binding binding = new Binding("DataModel.DetailList[" + row + "].IsCorrect")
        {
            Path = new PropertyPath("DataModel.DetailList[" + row + "].IsCorrect"),
            Mode = BindingMode.TwoWay
        };
        return binding;
    }

实现绑定的代码如下:

Binding cbBind = SetIsCorrectBinding(row);
detailRow.IsCorrect_cb.SetBinding(CheckBox.ContentProperty, cbBind);

无论我尝试什么,IsCorrect 变量始终为假。 对此的任何帮助将不胜感激。

【问题讨论】:

  • 你想绑定什么? true/false 似乎意味着bool,通常会绑定到IsChecked。您正在绑定到Content 属性,这会影响CheckBox 的显示方式...绑定到bool 类型,我希望控件显示当前的bool 值(例如"False"),并且不会期望源属性会被更改(因为 CheckBox 不是编辑控件)。根本不清楚您要做什么。请澄清您的问题。

标签: c# wpf checkbox data-binding devexpress


【解决方案1】:

请尝试下一个:

    var xBinding = new Binding();
    //a real instance of the object where the source property is defined
    //it have to be the same instance which is defined in DataModel.DetailList
    xBinding.Source = sourceInstance;
    xBinding.Path = new PropertyPath("The_source_property_name");
    xBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    xBinding.Mode = BindingMode.TwoWay;
    //Use this instead the .SetBinding( , ) where the checkbox is the object to binded to
    BindingOperations.SetBinding(checkbox, CheckBox.ContentProperty, xBinding);

请查看下一个解决方案here,我认为它可以为您提供额外的信息。 如果您遇到代码问题,我很乐意提供帮助。 问候,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2012-10-29
    • 2010-11-27
    • 1970-01-01
    • 2010-10-11
    • 2016-10-24
    相关资源
    最近更新 更多