Question

I have re-styled a ListBox to create a RadioButtonList from this link

My requirement is something like this : I have to select a single item from the ListBox at a time(ie Single Selection Mode). Also I have to disable/enable ListBoxItem based on property that bind to the collection. So I have set

IsChecked="{TemplateBinding IsSelected}"

and bound the IsEnabled property from my collection.

IsEnabled="{Binding IsEnabled}" 

And the result as follows: enter image description here

You can see that some of the records are in disabled state, but still they are selectable.If I remove the IsChecked property, it works perfectly as expected. But I need both the IsEnabled & IsSelected functionlaity. Then I create a multivalue converter for the IsEnabled property and based on the values I have bind the corresponding value to the property. Now I can't select a disabled item from list visually. But when I select a disabled item, I loss the selection. Please check the image : enter image description here.

and in code behind the IsChecked property is set to the first record. I want to restrict this selection. How can I do that? Is there any settings in xaml will help to do my requirements? Please advice...

Thanks in advance....

Was it helpful?

Solution

It sounds like you're binding the IsEnabled property of the RadioButton instead of the ListBoxItem. This would disable the RadioButton, but not the ListBoxItem, which is why it can still be selected.

You should be able to bind the IsEnabled property of the ListBoxItem, and it will work the way you want. 

Based on the link you posted going to the style you're using, that would be in the ItemContainerStyle section of the RadioButtonList style:

<Style TargetType="{x:Type ListBoxItem}" >
    <!-- Here -->
    <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />

    <Setter Property="Margin" Value="5" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border BorderThickness="0" Background="Transparent">
                    <RadioButton Focusable="False"
                                 IsHitTestVisible="False"
                         IsChecked="{TemplateBinding IsSelected}">
                        <ContentPresenter />
                    </RadioButton>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top