WPF Databound RadioButton ListBox
Posted
by Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-02-04T17:41:58Z
Indexed on
2010/04/10
8:03 UTC
Read the original article
Hit count: 905
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.
- Mark
© Stack Overflow or respective owner