0

I'm trying to figure out how to create a user control that would act as a modal group of togglebuttons. I don't want to go the radio button route because the user has to be able to deselect everything. I also need the list to support vertical and horizontal presentation.

I'd like to have a control that looks like this in XAML.

<MyControl>
    <Grid>
        <Grid.ColumnDefinitions>...</Grid.ColumnDefinitions>
        <ToggleButton />
        <ToggleButton />
        <ToggleButton />
        ...
        <ToggleButton />
    </Grid>
</MyControl>

I think grid may not be the correct layout option here since I don't know how many buttons I'll have.

Ggilmann
  • 55
  • 1
  • 5
  • Possible duplicate of [How to get a group of toggle buttons to act like radio buttons in WPF?](https://stackoverflow.com/questions/2362641/how-to-get-a-group-of-toggle-buttons-to-act-like-radio-buttons-in-wpf) – ASh Aug 21 '17 at 14:36
  • Like I said, I don't want it to behave like a group of radio buttons, plus I'm unsure if the solution in that question provides support for vertical and horizontal – Ggilmann Aug 21 '17 at 15:19
  • another one: https://stackoverflow.com/questions/32719839/how-to-uncheck-radio-button-in-wpf. choose correct ItemsPanel for ListBox and you will have support for vertical and horizontal presentation. ListBox has selection support out-of-box. IMO it is better to heavily customize ListBox than hardcode some manipulations with toggle button children of grid sender – ASh Aug 21 '17 at 17:19

1 Answers1

0

You could handle the routed Checked event according to the following sample code.

XAML:

<Grid ToggleButton.Checked="Grid_Checked">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <ToggleButton Content="A" />
    <ToggleButton Content="B" Grid.Column="1" />
    <ToggleButton Content="C" Grid.Column="2" />
</Grid>

Code:

private void Grid_Checked(object sender, RoutedEventArgs e)
{
    Grid grid = sender as Grid;
    ToggleButton toggledButton = e.OriginalSource as ToggleButton;
    IEnumerable<ToggleButton> allToggleButtons = grid.Children.OfType<ToggleButton>();
    foreach (ToggleButton toggleButton in allToggleButtons)
        if (toggleButton != toggledButton)
            toggleButton.IsChecked = false;
}

Only one ToggleButton in the Grid can be checked simultaneously, but all ToggleButtons may be unchecked.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • That looks like it may help. I'll try to test it out today. – Ggilmann Aug 21 '17 at 15:20
  • What about showing it horizontal or veritcal? Is there a way, with templates maybe, to redesign the grid but keep the buttons in the same place? Maybe with a content presenter in the right place? – Ggilmann Aug 21 '17 at 15:31
  • Please ask a new question if you have another issue. One question is one question. – mm8 Aug 21 '17 at 15:35
  • Then the original question is way to broad and should be closed. You shouldn't ask about layout *and* modal ToggleButtons in the same question. – mm8 Aug 21 '17 at 15:55