IsSelected binding on RadListBox

1 Answer 271 Views
ListBox
Ross
Top achievements
Rank 2
Iron
Ross asked on 03 Aug 2022, 12:40 AM | edited on 03 Aug 2022, 12:43 AM

Hi

I am having an issue binding the IsSelected property of a RadListboxItem. Specifically I am attempting to follow the pattern provided in this blog post Accessing WPF ListBox SelectedItems using MVVM (markwithall.com)

The key part toward the bottom of the post is this which binds the IsSelected property of a ListBoxItem. I have tested it and it works just fine.

<ListBox ItemsSource="{Binding Items}" SelectionMode="Extended">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

However when I try to use a RadListbox and setup the ItemContainerStyle like this

<telerik:RadListBox.ItemContainerStyle>
	<Style TargetType="telerik:RadListBoxItem" BasedOn="{StaticResource RadListBoxItemStyle}">
		<Setter Property="IsSelected" Value="{Binding IsSelected}" />
	</Style>
</telerik:RadListBox.ItemContainerStyle>
The bound value does not update, is this by design or a bug?
Stenly
Telerik team
commented on 04 Aug 2022, 03:33 PM

I have tested setting the IsSelected property of the RadListBoxItem element via a style for the ItemContainerStyle, however, on my end the items are correctly selected. I have attached a sample application, so, could you give it a try?
Ross
Top achievements
Rank 2
Iron
commented on 04 Aug 2022, 10:35 PM

Hi Stenly, thankyou for the example. It shows the binding from source works, I wanted to see two way binding work. I realised that I had not specifically set Mode=Twoway, I did not think I had to since it was working fine in the Listbox example. I have now updated the binding to 


<Style TargetType="telerik:RadListBoxItem" BasedOn="{StaticResource RadListBoxItemStyle}">
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
And it is working perfectly, thanks for your help

1 Answer, 1 is accepted

Sort by
0
Ross
Top achievements
Rank 2
Iron
answered on 12 Aug 2022, 02:58 AM
To fix this all that is needed is to set the binding mode to TwoWay against the IsSelected binding.
Tags
ListBox
Asked by
Ross
Top achievements
Rank 2
Iron
Answers by
Ross
Top achievements
Rank 2
Iron
Share this question
or