c# - 双向绑定(bind)不起作用

标签 c# wpf silverlight data-binding binding

在我的 Silverlight 项目中,我正在创建文本框,这些文本框在运行时双向数据绑定(bind)到某个上下文。一个方向(从源到目标)的绑定(bind)似乎工作正常,但另一个方向(从目标回到源)没有显示任何效果.

这是数据上下文:

public class Leg : INotifyPropertyChanged {

    private string passengers;

    public string Passengers {
        get { return passengers; }
        set {
            // here I have a breakpoint.
            passengers = value;
            FirePropertyChanged("Passengers"); 
         }
    }

    private void FirePropertyChanged (string property) {

        if (PropertyChanged != null) {

            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

然后在另一个地方我正在创建一个新的 TextBox 控件以及它的绑定(bind):

Binding passengersBinding = new Binding();

// viewModelLeg is an instance of the class Leg from above    
passengersBinding.Source = viewModelLeg;

passengersBinding.Path = new PropertyPath("Passengers");
passengersBinding.Mode = BindingMode.TwoWay;

legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);

现在,当我更改 Passengers 字符串的值时,绑定(bind)到它的相应文本框会正确更新其文本。 所以一切都很好。

但是当我手动更改文本框的文本然后使文本框失去焦点时,没有任何反应 - 即没有发生双向绑定(bind) - 文本框的新文本值没有向下传播到来源!

我在 passengers-attribute 的 setter 处设置了一个断点(用上面的断点注释标记)。 当我得到所有这些权利时,当绑定(bind)的目标值已更改以更新源时,绑定(bind)引擎也会使用此公共(public) setter - 所以当发生这种情况时,必须命中断点。 但他没有!所以看来我可以用我的文本框做我想做的事(玩焦点或按回车)它永远不会更新它的源。

我在监督什么吗?在我的代码或我的想法中一定有一个严重的错误..我真的很感激任何想法......

编辑:

在下文中,我将尝试演示如何创建我的 XAML 对象和我的 DataContext 对象。因为我在运行时创建 XAML 控件及其绑定(bind),所以我还没有找到很好的解决方案来很好地实现 MVVM 方法。所以我正在做以下事情(这可能不是最好的方法):

我建模的情况是我有一个 UserControl(称为 LegItem),它(主要)由文本框组成。在运行时,用户可以根据自己的意愿创建尽可能多的用户控件(一个接一个)。

在我的 ViewModel 端,我有一个类(称为 Leg)作为一个 LegItem 的 ViewModel。所以当我说 n (XAML-) LegItems 时,我也有 n Leg 实例。我将这些 Leg 对象存储在一个列表中。

因此,每当用户点击“添加一条新腿”按钮时,我都会执行以下操作:

// here we are inside the applications view in an .xaml.cs file

public void AddLeg () {

    // this is going to serve as the ViewModel for the new LegItem
    // I am about to create.
    Leg leg = viewModel.insertLeg();

    // here I am starting to create the visual LegItem. The ViewModel object
    // I have created in the previous step is getting along with.
    createLegItem(leg);
}

// the primary job here is to bind each contained textbox to its DataContext.
private LegItem createLeg (Leg viewModelLeg) {

    // create the visual leg item control element
    // which is defined as a XAML UserControl.
    LegItem legItem = new LegItem();

    Binding passengersBinding = new Binding();

    // viewModelLeg is an instance of the class Leg from above    
    passengersBinding.Source = viewModelLeg;

    passengersBinding.Path = new PropertyPath("Passengers");
    passengersBinding.Mode = BindingMode.TwoWay;

    legItem.paxTextBox.SetBinding(TextBox.TextProperty, passengersBinding);

}


// on the viewModel side there is this simple method that creates one Leg object
// for each LegItem the View is creating and stores inside a simple list.

public Leg InsertLeg () {

    Leg leg = new Leg();
    legList.add(leg)

    return leg;
}

最佳答案

新答案

由于您提到您的绑定(bind)实际上是自定义 UserControl 而不是 TextBox,我建议查看您的 UserControl 的 XAML > 并确保它正确绑定(bind)数据


旧答案

我对新的 Silverlight 项目进行了快速测试,发现启动项目是 SilverlightApplication1.Web,而不是 SilverlightApplication

这意味着当我运行项目时,setter 中的断点实际上不会被命中。你会注意到断点圆只是轮廓,没有填充颜色。如果将鼠标悬停在它上面,它会显示

The breakpoint will not currently be hit. No symbols have been loaded for this document

如果我启动 SilverlightApplication1 而不是 .Web 版本,断点就会命中。

无论我启动哪个版本,属性都是正确更改,但是如果我使用.Web 版本启动项目,断点不会被击中。我怀疑这是你的问题。

关于c# - 双向绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928996/

相关文章:

silverlight - S/L 4 & IDataErrorInfo - 如何强制重新验证控件(触摸相关控件时)

C#/WPF - 重新编译 SilverLight 4 工具包以用于 WPF 应用程序是否可行?

c# - 在 Azure 逻辑应用中转换 XML,将 C# 程序集 (.DLL) 文件上传到 Azure 集成帐户并在 XSLT 中使用它(映射)

c# - 使用 ToString() 格式化时舍入十进制和 double 之间的差异

c# - 如何重构以通过 try/multiple catch 减少嵌套深度 (NDepend)

c# - 为什么我的转换器给出无效的转换错误?

c# - View 在不同的 Assembly 中找不到 ViewModel

c# - app.manifest 和 assemblyInfo.cs 版本号以及是否需要 app.manifest?

c# - WPF 两个 Line 对象的交点坐标

c# - 拆分 app.xaml 的最佳方法是什么?