C# 数据绑定TextBox TextChanged不工作
我有两个文本框(XAML) 而getcommunies(字符串搜索)是一个Linq查询C# 数据绑定TextBox TextChanged不工作,c#,wpf,linq,data-binding,textchanged,C#,Wpf,Linq,Data Binding,Textchanged,我有两个文本框(XAML) 而getcommunies(字符串搜索)是一个Linq查询 db = new MyModelContainer(); CommunesList.Clear(); Commune.DataContext = _CommunesList; var myCommunes = from d in db.Communes where d.CommunePostale.Equals(search) selec
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上的getCommunies()方法时
private void CodePostalChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
getCommunes("Toto");
}
什么也没有发生,文本框被清除
(它应该显示其他内容,但即使CommunieList有一个元素,它也是空的)因为您没有将文本框绑定到良好的属性CommunieGeographique
<s:SurfaceTextBox Grid.Column="1" Name="Commune" Text="{Binding Path=CommuneGeographique}" />
当我第一次启动应用程序时,它就起作用了,问题是当我在CodePostal文本框中键入内容时,Commune文本框被清除,并且没有填入正确的数据!!您好,Wassim,因为您的绑定没有经过调整,所以您必须使用对象中属性的path=名称进行绑定。例如,调用您的函数,在对象的属性(TestProperty)中设置结果,并将文本框与TestProperty绑定,对于我们的示例,这正是我正在做的。CommuniteGeographique是db对象的一个属性。如果不是,我至少一次都不应该让它工作你实现了INotifyPropertyChanged吗?不,你能帮我吗?
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;
private void CodePostalChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
getCommunes("Toto");
}
<s:SurfaceTextBox Grid.Column="1" Name="Commune" Text="{Binding Path=CommuneGeographique}" />