【问题标题】:Textbox + binding to Property with INotifyPropertyChanged from different assembly文本框 + 绑定到来自不同程序集的 INotifyPropertyChanged 的​​属性
【发布时间】:2011-05-11 12:39:13
【问题描述】:

我在从绑定到文本框的代码中更新属性值时遇到问题(文本框不显示新值)。

它正常工作没有任何问题,但在这种情况下,源类在单独的程序集中(不知道它是否有任何区别)。

  1. 如果我在文本框中输入,值将在代码中更新
  2. 当我直接在代码文本框中更改属性值时,不显示新值。
  3. 再次更改文本框中的值后,先前设置的值(在代码中)被覆盖(因此绑定仍然有效)。

此外,我检查了 PropertyChanged 事件是否被触发,并且是在每次更改之后。

任何猜测为什么它不起作用?下面对应的绑定和源类。

TextBox Text="{Binding Path=Description, Mode=TwoWay}"

[DataContract]
public class Source : INotifyPropertyChanged
{
    private String _Description;

    [DataMember]
    public String Description
    {
        get { return _Description; }
        set
        {
            if (_Description == value)
                return;

            _Description = value;
            OnPropertyChanged("Description");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
}

我已经修复了它显然没有连接到不同组件的问题。我在为属性设置值时遇到错误。

【问题讨论】:

  • 您是否尝试过在 OnPropertyChanged 中设置断点并查看是否有任何内容附加到 PropertyChanged 事件?这应该告诉您是否正在通知 TextBox。

标签: c# wpf textbox binding


【解决方案1】:

对我来说效果很好。唯一的区别是删除了 DataContract 标记并分配了 DataContext。我的示例如下:

<Window x:Class="_4206499.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="360" Width="578">
<Grid>
    <TextBox Text="{Binding Path=Description, Mode=TwoWay}" VerticalAlignment="Center" Margin="12,12,286,278" />
    <Button Width="100" Margin="57,59,346,219" Click="Button_Click"></Button>
</Grid>

后面的代码是

using System;
using System.Windows;
using ClassLibrary1;

命名空间_4206499 { 公共部分类 MainWindow : 窗口 { 公共主窗口() { 初始化组件(); 来源 = 新来源(); 数据上下文 = 源; }

    public Source Source { get; set;}

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Source.Description = DateTime.Now.ToString();
    } 
}

}

和一个来自单独的类库:


using System;
using System.ComponentModel;

namespace ClassLibrary1 { public class Source : INotifyPropertyChanged { private String _Description;

    public String Description
    {
        get { return _Description; }
        set
        {
            if (_Description == value)
                return;

            _Description = value;
            OnPropertyChanged("Description");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
}

}

【讨论】:

  • 我把我的问题简化了太多,没有检查它是否仍然无法正常工作。我使用 ICustomTypeDescriptor 来重定向属性,这就是绑定只能以一种方式工作的原因。通过重定向 OnPropertyChanged 事件也解决了该问题。无论如何感谢您的帮助。
猜你喜欢
  • 2019-03-21
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多