【问题标题】:data binding TextBox TextChanged is not working数据绑定 TextBox TextChanged 不起作用
【发布时间】:2012-07-04 15:25:43
【问题描述】:

我有两个文本框 (XAML)

<s:SurfaceTextBox Name="CodePostal" TextChanged="CodePostalChanged" />
<s:SurfaceTextBox Name="Commune" Text="{Binding Path=CommuneGeographique}" />

并且在(.cs 文件

private MyModelContainer db;
private ObservableCollection<Commune> _CommunesList = new ObservableCollection<Commune>();
private ObservableCollection<Commune> CommunesList 
{ 
    get { return _CommunesList; }
    set { _CommunesList = value; }
}

构造函数中,我有这个:

InitializeComponent();
getCommunes("Test");

getCommunes(string search),是一个Linq查询

db = new MyModelContainer();

CommunesList.Clear();
Commune.DataContext = _CommunesList;

var myCommunes = from d in db.Communes
                 where d.CommunePostale.Equals(search)
                 select d;

foreach (Commune c in myCommunes)
{
    CommunesList.Add(c);
}

Commune.DataContext = CommunesList;

此时一切正常,Commune 文本框显示我想要的内容。

我的问题是当我尝试在 TextChanged 上调用 getCommunes() 方法时

private void CodePostalChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
    getCommunes("Toto");
}

什么都没有发生,文本框被清除。

(它应该显示其他内容,但即使 CommuneList 有元素,它也是空的)

【问题讨论】:

    标签: c# wpf linq data-binding textchanged


    【解决方案1】:

    因为您没有将文本框绑定到良好的属性 CommuneGeographique ??

      <s:SurfaceTextBox Grid.Column="1" Name="Commune" Text="{Binding Path=CommuneGeographique}" />
    

    【讨论】:

    • 它在我第一次启动应用程序时工作,问题是当我在 CodePostal TextBox 中输入内容时,Commune TextBox 被清除并且没有填充正确的数据!
    • 您好 Wassim,因为您的绑定未调整,您必须在您的对象中使用 path = 您的属性名称进行绑定。例如调用您的函数,在对象的属性(TestProperty)中设置结果,并将您的文本框与 TestProperty 绑定,例如
    • 这正是我正在做的。 CommuneGeographique 是 db 对象的属性。如果不是,我不应该让它至少工作一次
    • 你实现了 INotifyPropertyChanged 吗?
    • 不,你能帮我做吗?
    猜你喜欢
    • 1970-01-01
    • 2017-02-15
    • 2020-10-22
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    相关资源
    最近更新 更多