0
votes

I'm using radiobuttons inside a listbox and cannot disable them. After searching I found links about IsEnabled and IsHitTestVisible. When I set IsEnabled to false the button and text turns grey however it's still clickable. Setting IsHitTestVisible does nothing to change this. My XAMLcode looks like this:

<ListBox ItemsSource="{Binding Source}" BorderThickness="0" VerticalAlignment="Stretch" Background="Transparent" SelectedItem="{Binding SelectedSource, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <RadioButton IsHitTestVisible="{Binding IsEnabled}"  IsEnabled="{Binding IsEnabled}" Margin="0 2 0 0" FontSize="12" FontFamily="Segoe UI Regular" Content="{Binding name}" GroupName="GroupList"  >
                <RadioButton.Style>
                    <Style TargetType="{x:Type RadioButton}">
                        <Setter Property="IsChecked" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
                    </Style>
                </RadioButton.Style>
            </RadioButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox> 

Property in c# that calculates and returns bool.

public bool IsEnabled
{     
    get
    {
        IsEnabledCalculate();
        return isEnabled;
    }
}

Any ideas about how to disable selection?

1
Where is you INotifyPropertyChange implementation? this property will never notify the UI that there is a change without it.adminSoftDK

1 Answers

1
votes

Using IsHitTestVisible on a RadioButton in ListBox was a good start. This way, when you click the RadioButton, the event goes "through" it and is handled by ListBoxItem (the item gets selected). You can then manipulate IsChecked property by binding it do IsSelected property of the ListBoxItem.

This is however still not enough to achieve waht you want. First, you're binding your IsEnabled property on the wrong level: the RadioButton. As you noticed, this still allows the ListBoxItem to get selected. So, you need to bind your IsEnabled property to the IsEnabled property of ListBoxItm instead:

        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="IsEnabled"  Value="{Binding Path=IsEnabled}"/>
            </Style>
        </ListBox.ItemContainerStyle>

There still is one minor issue here. When your IsEnabled property is changed in the codebehind, the ListBoxItem will get disabled but the RadioButton will still be checked. To prevent this and uncheck the RadioButton whenever the item gets disabled you can use a DataTrigger:

           <DataTemplate>
                <RadioButton GroupName="GroupList" IsHitTestVisible="False"
                     Margin="0 2 0 0" FontSize="12" FontFamily="Segoe UI Regular" 
                     Content="{Binding name}" >
                    <RadioButton.Style>
                        <Style TargetType="{x:Type RadioButton}">
                            <Setter Property="IsChecked" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsEnabled}" Value="False">
                                    <Setter Property="IsChecked" Value="False"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RadioButton.Style>
                </RadioButton>
            </DataTemplate>