在代码中手动设置属性后丢失单选按钮的绑定
我的 WPF xaml 中有一个列表,其中包含两个项目。以下是每个项目的样式模板。现在在用户界面上,它显示为一组单选按钮(单选按钮的数量取决于我的列表中的项目数量)。
<Style x:Key="RadioButtonListBoxItemStyle" TargetType="{x:Type ListBoxItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<RadioButton FlowDirection="LeftToRight"
Margin="10 15"
Content="{Binding Value}"
GroupName="{Binding DisplayGroupName}"
IsChecked="{Binding IsSelected, Mode=TwoWay}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在我使用上面的样式模板绑定一个列表(有 2 个项目)来获取两个单选按钮。发生的情况是,一切都工作得很好,即当我更改 UI 上单选按钮的选择时,IsSelected 属性会根据我的单选按钮是否选中/取消选中而正确更新为 true 或 false。但是,如果我尝试在代码中手动设置列表,那么从那时起,我的单选按钮与列表的绑定就会丢失,并且不会发生任何事情。
对此的任何帮助都会很棒,根据我的需要,我必须手动在代码中设置列表。那么,即使我在代码中手动设置列表,是否有任何解决方案可以使绑定不会丢失。谢谢。
-阿迪。
I have a list on my WPF xaml which contains two items. Below is the Style template for each item. Now on UI this shows like a group of radio buttons(No. of radio buttons depends on no. of items in my list).
<Style x:Key="RadioButtonListBoxItemStyle" TargetType="{x:Type ListBoxItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<RadioButton FlowDirection="LeftToRight"
Margin="10 15"
Content="{Binding Value}"
GroupName="{Binding DisplayGroupName}"
IsChecked="{Binding IsSelected, Mode=TwoWay}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now I bind a list(having 2 items) using the above style template to get two radio buttons. What happens is everything works pretty fine i.e when I change the selection of radio button on UI the IsSelected property is getting updated properly to true or false depending on whether my radio is checked/un-checked. But if I try to set the list in the code manually, then from that point my binding of the radio button's with my list is lost and nothing happen's.
Any help on this would be great and based on my needs I have to set the list in the code manually. So is there any solution in a way that binding will not be lost even though I set the list in my code manually. Thanks.
-Ady.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 WPF 中单选按钮的一个常见问题,它必须执行绑定的一个不寻常的方面,即功能多于错误。
绑定的设计假设只有两件事会更改绑定的目标属性的值:a) UI 中的操作和 b) 对源属性的更改。如果您在代码中设置绑定的 target 属性 - 就像您显式设置
Border
的Background
一样,即使它具有绑定- 绑定决定您知道自己在做什么,并且它应该避开。所以它会自行关闭。在大多数情况下,这是一个非常明智的设计决策。例如,这比抛出异常更好。大多数时候,您无论如何都不会在代码中设置
IsEnabled
;你会让绑定来完成它。特别是如果您使用 MVVM。好的,如果组中有单选按钮会怎样?
当您选中组中的一个按钮时,管理单选按钮组的 WPF 代码会通过在代码中将
IsChecked
设置为 false 来取消选中该组中的所有其他按钮。绑定会自行禁用。哎呀。解决方案如下:如果您使用单选按钮和绑定,请不要使用组。处理视图模型代码中的互斥逻辑。在您的情况下,对视图模型进行编码,以便集合中只有一个对象在任何给定时间都可以具有
IsSelected
true 。 (是的,这很痛苦。)单选按钮仍将按预期工作,但由于代码设置的唯一属性是源属性,因此绑定不会中断。
This is a common problem with radio buttons in WPF, and it has to do an unusual aspect of binding, one that is marginally more feature than bug.
The design of binding assumes that the only two things that change the value of a binding's target property are a) actions in the UI and b) changes to the source property. If you set the target property of a binding in code - like, you explicitly set the
Background
of aBorder
, even though it has a binding - the binding decides that you know what you're doing, and that it should just get out of the way. So it turns itself off.This is a pretty sensible design decision, for the most part. It's better than throwing an exception, for instance. Most of the time, you're not going to ever set
IsEnabled
in code anyway; you'll let the binding do it. Especially if you're using MVVM.Okay, so what happens if you have radio buttons in a group?
When you check one button in the group, the WPF code that manages radio button groups unchecks all the other buttons in the group, by setting
IsChecked
to false in code. The binding disables itself. Oops.Here's the solution: If you're using radio buttons and binding, don't use groups. Handle the mutual exclusion logic in your view model code. In your case, code your view models so that only one object in a collection can have
IsSelected
true at any given time. (Yes, this is a pain.)The radio buttons will still work as expected, but since the only properties being set by code are the source properties, binding won't break.
您正在设置 listboxitem 类的样式,包括绑定。因此,当您从后面的代码设置列表时,它不包含列表框项目,但它包含列表中的项目。因此,该样式不适用。您应该做的是为列表中的项目类型创建
——实际上告诉 WPF 您希望每个项目是什么样子。(这超出了我的想象,所以 xaml 可能不完全正确)
you are setting the style for the listboxitem class, including the bindings. so, when you set the list from code behind it does not contain listboxitems, it contains the items from your list. so, the style does not apply. what you should do is make the
<DataTemplate>
for the type of item in your list--in effect telling WPF what you want each item to look like.(this is off the top of my head, so the xaml might not be exactly right)