WPF 数据绑定单选按钮列表框
我在 WPF 中获取数据绑定单选按钮列表框以响应用户输入并反映对其绑定的数据的更改(即在代码中进行更改)时遇到问题。用户输入端工作正常(即,我可以选择一个单选按钮,并且列表的行为符合预期)。但每次尝试更改代码中的选择都会失败。默默地(即,也不例外)。
这是 XAML 的相关部分(我认为):
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Name="theBorder" Background="Transparent">
<RadioButton Focusable="False" IsHitTestVisible="False"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" >
<ContentPresenter />
</RadioButton>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
我将列表框绑定到 SchoolInfo 对象列表。 SchoolInfo 包含一个名为 IsSelected 的属性:
public bool IsSelected
{
get { return isSelected; }
set
{
if( value != isSelected )
{
isSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
}
OnPropertyChanged() 内容是我在实验期间放入的内容。它并不能解决问题。
像下面这样的事情会失败:
((SchoolInfo) lbxSchool.Items[1]).IsSelected = true;
lbxSchool.SelectedIndex = 1;
它们默默地失败——不会抛出异常,但 UI 不会显示正在选择的项目。
I am having problems getting a databound radiobutton listbox in WPF to respond to user input and reflect changes to the data it's bound to (i.e., to making changes in the code). The user input side works fine (i.e., I can select a radiobutton and the list behaves as expected). But every attempt to change the selection in code fails. Silently (i.e., no exception).
Here's the relevant section of the XAML (I think):
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Name="theBorder" Background="Transparent">
<RadioButton Focusable="False" IsHitTestVisible="False"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" >
<ContentPresenter />
</RadioButton>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
I bind the listbox to a List of SchoolInfo objects. SchoolInfo contains a property called IsSelected:
public bool IsSelected
{
get { return isSelected; }
set
{
if( value != isSelected )
{
isSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
}
The OnPropertyChanged() stuff was something I put in during my experimentation. It doesn't solve the problem.
Things like the following fail:
((SchoolInfo) lbxSchool.Items[1]).IsSelected = true;
lbxSchool.SelectedIndex = 1;
They fail silently -- no exception is thrown, but the UI doesn't show the item being selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RadioButton 绑定到 ListBoxItem IsSelected 属性,而不是您的 SchoolInfo IsSelected 属性。
(这很令人困惑,因为 ListBoxItem 有一个“IsSelected”属性,您的 SchoolInfo 对象也有“IsSelected”属性,这就是没有绑定错误的原因)。
要修复此问题,ListBoxItem.IsSelected 需要绑定到您的 SchoolInfo IsSelected 属性。
即您需要一个额外的 setter 来将 ListBoxItem 绑定到 SchoolInfo.IsSelected,然后列表框项目将按其应有的方式工作,并且 RadioButton 也可以正确绑定到 ListBoxItem.IsSelected。
The RadioButton is binding to the ListBoxItem IsSelected property, not to your SchoolInfo IsSelected property.
(It is confusing because ListBoxItem has an "IsSelected" property, and so also does your SchoolInfo object, which is why there were no binding errors).
To fix, ListBoxItem.IsSelected needs to be bound to your SchoolInfo IsSelected property.
i.e. You need an extra setter for the ListBoxItem to bind to SchoolInfo.IsSelected, and then the list box item will work as it should, and also the RadioButton can bind correctly to ListBoxItem.IsSelected.
我将在您的 RadioButton 绑定中启用 NotifyOnSourceUpdated。即使您允许双向绑定(这是默认设置),也不会从代码隐藏更改中获取通知,除非您显式侦听它们。
I would enable NotifyOnSourceUpdated in your RadioButton binding. Even though you are allowing a two-way binding (which is the default), notifications won't be picked up from code-behind changes unless you are explicitly listening for them.