绑定问题让我发疯

时间:2009-10-02 19:12:52

标签: wpf vb.net binding combobox

我(以及其他论坛的其他程序员)已经尝试解决绑定问题几天了。没人能帮忙......这里的任何人都可以帮助我吗?显然,这个问题看起来很简单但是当你潜入它时,相信我,你会燃烧一些神经元...... 这是:我创建了一个可以重现问题的测试项目。从here下载。 运行项目并:

  • 从列表框中选择一个成员

  • 在文本框中输入邮政编码 (这些是法语ZIP,从01000起 至98000)

  • 在组合框中选择一个城市

  • 选择其他成员并执行相同操作 (然后使用不同的邮政编码) 选择回前一个成员 并重新选择另一个=>看到 member.Ville.ID属性如何 失去了,所以如何 组合框中的SelectedItem是 也输了。

为什么这样做呢? 怎么解决这个问题?

P.S:对于班级物业和其他地方的法国人感到抱歉......

2 个答案:

答案 0 :(得分:1)

有些东西正在设定这个价值。我没有调查太多,但我在你的绑定上放了一个绑定诊断(这里是你自己如何做到这一点:here)并得到了这个:

Update - got raw value '9800'
Update - using final value '9800'
SetValue at level 1 to Ville (hash=15263193) using RuntimePropertyInfo(CodePostal): '9700'
Got PropertyChanged event from Ville (hash=15263193)
Update - got raw value '97001'
Update - using final value '97001'
SetValue at level 1 to Ville (hash=15263193) using RuntimePropertyInfo(CodePostal): '97001'
Got PropertyChanged event from Ville (hash=15263193)
Update - got raw value '01000'
Update - using final value '01000'
SetValue at level 1 to Ville (hash=15263193) using RuntimePropertyInfo(CodePostal): '01000'
Got PropertyChanged event from Ville (hash=15263193)

请注意,一旦我点击了97001中的最后一个“1”,就会出现一个PropertyChanged通知,将其重置回01000,所以有些东西将其重新设置为。

我在代码的Set方法中为“value.Equals(”01000“)输入了一个条件断点,并获得了发生时的堆栈跟踪:

TestMembre.exe!TestMembre.Ville.set_CodePostal(String value = "01000") Line 47  Basic
TestMembre.exe!TestMembre.Window1.txtCodePostal_TextChanged(Object sender = {System.Windows.Controls.TextBox}, System.Windows.Controls.TextChangedEventArgs e = {System.Windows.Controls.TextChangedEventArgs})

所以你应该注意的是txtCodePostal的TextChanged事件有一些设置该值的代码:

Else
   'If the zip code doesn't exist, it is set to one that does
   txtCodePostal.Text = "01000"

我不确切知道你的逻辑应该如何工作,但那段代码总是触发并重置你的值。

希望这有帮助!

答案 1 :(得分:0)

由于您的链接,您的消息帮助了我调试Binding的方式。 请注意,对我来说,txtCodePostal.Text = "01000"从未发生过,因为我总是键入正确的邮政编码。仅当用户键入不存在的邮政编码时才会发生这种情况。虽然,我已经使用Bea Stollnitz网站的Trace Sources调试我的绑定。我已将其保存到this txt file。当列表框中的SelectionChanged发生时,会发生一些错误。我还没有理解所有的东西,但我正在尝试......如果你能看一下这个文件,那就太好了......