Is it possible to 'refresh' WPF data bindi

2020-02-11 06:24发布

I've got a xaml TabControl and on one page, there are 3 RadioButtons each bound to a different property on the selected value of an adjacent ListView. After switching between selected items in the ListView, my radio buttons seem to forget they're bound and don't refresh.

So watching it in the debugger, when I switch to a new selected item, I see the non user code first checking the value on all 3 properties, then only the first two, and eventually only the first. However, if I change the tab and change back, it seems to give me another few uses.

The binding itself is fairly straightforward. a TwoWay binding of a bool property to IsChecked. It's 4 levels deep (Path=DataModel.Selected.A.B), but I have other things at the same depth that work fine.

Is this something people have heard about and know what might be going on? Or if the binding is somehow getting forgotten, is there a way to explicitly remind the xaml?

3条回答
别忘想泡老子
2楼-- · 2020-02-11 07:14

This is apparently somewhat of a known issue:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8eb8280a-19c4-4502-8260-f74633a9e2f2/

In short, a RadioButton (through .Net 3.5sp1) somehow kills bindings of other RadioButtons when when it's checked while trying to uncheck any other buttons. The simple fix (read: hack) is to assign each radiobutton a different GroupName and then they don't try to mess with eachother

查看更多
放荡不羁爱自由
3楼-- · 2020-02-11 07:21

It is possible to manually update bindings like this:

TestCheckBox
    .GetBindingExpression(CheckBox.IsCheckedProperty)
    .UpdateTarget();

That being said, I don't have 100% confidence that this will correct your underlying issue. I haven't had this sort of issue before with WPF bindings, but I have had a couple weird issues with the tab control.

查看更多
家丑人穷心不美
4楼-- · 2020-02-11 07:25

Another way to resolve this issue is to fake up a list of properties in a ListBox and have the ListBoxItem template be a radiobutton.

查看更多
登录 后发表回答