Question

I have a ListBox that contains Items comprised of RadioButtons. They are created dynamically by providing the ItemsSource for it. Here is the XAML for the ListBox:

<ListBox 
   Margin="20,5,0,0" 
   Width="Auto" 
   Background="Transparent" 
   BorderThickness="0" 
   BorderBrush="Transparent" 
   DisplayMemberPath="DisplayValue" 
   SelectedValuePath="Value" 
   SelectedItem="{Binding Path=SingleAnswer}" 
   ItemsSource="{Binding Path=AnswerOptions}">
   <!-- KLG Score ItemsSource converter to filter out 'Unable to Assess' -->
   <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
   </ListBox.ItemsPanel>
   <ListBox.Resources>
      <Style TargetType="{x:Type ListBoxItem}">
         <Setter Property="Margin" Value="0,0,25,0" />
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type ListBoxItem}">
                  <Border Background="Transparent" BorderThickness="0" BorderBrush="Transparent">
                     <Border.IsEnabled>
                        <MultiBinding Converter="{StaticResource RadioButtonToEnabledConverter}" >
                           <Binding Mode="OneWay" ElementName="this" Path="ParentForm.ImagesExist"/>
                           <Binding Mode="OneWay" Path="." />
                        </MultiBinding>
                     </Border.IsEnabled>
                     <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Click="KLGScoreRadioButton_Click" >
                        <RadioButton.IsEnabled>
                           <MultiBinding Converter="{StaticResource RadioButtonToEnabledConverter}" >
                              <Binding Mode="OneWay" ElementName="this" Path="ParentForm.ImagesExist"/>
                              <Binding Mode="OneWay" Path="." />
                           </MultiBinding>
                        </RadioButton.IsEnabled>
                        <ContentPresenter />
                     </RadioButton>
                  </Border>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </ListBox.Resources>
</ListBox>

Now, one of the answers should not be able to be selected by the user, it should only be Auto-populated depending on what the Business logic specifies.

To do this, I created a converter for the RadioButton.IsEnabled property to always set the specified RadioButton disabled. This works; in the application it is properly disabled. However, if the user clicks on the disabled option, it is still allowing it to be selected. I've also tried using the same converter on the parent Border (as shown in the xaml above), however the behavior is unchanged.

I've also tried adding a listener on the Click event for the RadioButton to set it to handled if they are selecting the disabled option, but that event is not getting fired for some reason.

Is there anyway to not allow the user to select this option?

Was it helpful?

Solution

If you want a particular entry to not be selected you can bind IsHitTestVisible of that item in ListBoxItem style.

something like this,

<Setter Property="IsHitTestVisible" Value="{Binding IsSelectable}" />

where IsSelectable is your data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top