2

So, I hope the title is pretty self-explanatory but as always, gonna describe it better. I'm trying to put a lot of checkboxes into a list, so then I can use their index to uncheck them when I have another one checked.

A brief part of my XAML since it's kinda long and repetitive mostly

<Expander Header="Tercer Ciclo" FontWeight="Bold" Width="auto" Height="auto">
                <StackPanel>
                    <Expander Header="9° Grado" Margin="20,0,0,0">
                        <StackPanel>
                            <CheckBox x:Name="NovA" Margin="40,0,0,0" Content="Noveno A" />
                            <CheckBox x:Name="NovB" Margin="40,0,0,0" Content="Noveno B" />
                            <CheckBox x:Name="NovC" Margin="40,0,0,0" Content="Noveno C"/>
                            <CheckBox x:Name="NovD" Margin="40,0,0,0" Content="Noveno D"/>
                            <CheckBox x:Name="NovE" Margin="40,0,0,0" Content="Noveno E"/>
                        </StackPanel>
                    </Expander>

This structure just repeats itself another 7 times or so. The thing is, when I select one, I want the other ones to uncheck. It doesn't really affect the correct functioning of the program, it's just more aesthetic and to orient the user.

Before you start shouting that I should use that or this. I have tried:

  • Using radio buttons, but since it's a list IN another list, it didn't work out too well.
  • I searched how to create a list to insert the windows' controls, and found kind of an answer but it was meant for WinForms and I have no idea how to do it on WPF since it has differents methods.
  • Tried to insert the CheckBox's name manually, one by one... No way I'm finishing that; even then the foreach syntax for the controls...

I took this as a reference for the Code behind: [Assign checkboxes to an checkbox-array using a for loop in C# ]

I have this already, it was a code that I saw that was almost exactly what I wanted, but again, meant for WinForms so the foreach syntax kind of doesn't fit WPF

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        List<CheckBox> listCheck = new List<CheckBox>();
        foreach (Control checkbox in //Invoke the window's Controls, don't know the syntaxis)
        {
           //Add the Checkbox's x:Name on the list...


        }
    }

Sorry if I made this post so long, but I've been cracking my head around this for weeks now, and haven't found some clue to what I'm missing... I'm pretty new to WPF and C# in general, so yeah, kinda lost.

Rolin Azmitia
  • 129
  • 1
  • 2
  • 14

1 Answers1

1

You should start with creating a data object that contains :

  • A list of all available items
  • A property of the selected item

Once you have that, you can bind that to a control that is meant to display a list of items, and handle one item as selected. An ItemsControl is nice for displaying items, but doesn't track the selected item. A ListBox both displays a list of items, and tracks the selected item, so that's what I'd recommend here.

Here's an example style I've used in the past for displaying items in a ListBox using CheckBoxes :

<Style x:Key="CheckBoxListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <CheckBox IsHitTestVisible="False" Focusable="false" 
                                    Content="{TemplateBinding ContentPresenter.Content}"  
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

Because the ListBoxItem.IsSelected is bound to CheckBox.IsChecked, the second you select a different item it will uncheck everything else because IsSelected=False on all the other records.

And here's the ListBox XAML used for actually displaying the control :

<ListBox ItemsSource="{Binding ListOfAllItems}"
         Style="{StaticResource CheckBoxListBoxStyle}"
         SelectedItem="{Binding SelectedItem, Mode=TwoWay}">

Note that the .DataContext behind the ListBox should be whatever object you created that has the list of items and selected item. If you're unclear about how the DataContext is used in WPF, I'd suggest starting here : What is DataContext for?

Rachel
  • 130,264
  • 66
  • 304
  • 490