15

I am using C# and WPF and I basically want to have some toggle buttons and only one of them can be selected at the same time.

I found another question about that, but the solution shown there does not work and I don't know why.

If I try to do it as mentioned in the question above, the ItemTemplate of the ListBox is not applied. I just don't get the toggle buttons into the listbox, instead it is shown as a "normal listbox".

My toggle button style looks like this, contained in one of my resource files:

<Style x:Key="ToggleButtonListBox" TargetType="{x:Type ListBox}">
    <Setter Property="ListBox.ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <ToggleButton Content="{Binding}"
                          IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ListBox.ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="0" />
</Style>

I would like to add the items directly in the XAML code, so my code for that would look something like

<ListBox Style="{StaticResource ToggleButtonListBox}">
    <ListBoxItem>test1</ListBoxItem>
    <ListBoxItem>test2</ListBoxItem>
</ListBox>

How can I create such a set of buttons?

Community
  • 1
  • 1
Sören
  • 2,661
  • 2
  • 19
  • 22
  • possible duplicate of [How to get a group of toggle buttons to act like radio buttons in WPF?](http://stackoverflow.com/questions/2362641/how-to-get-a-group-of-toggle-buttons-to-act-like-radio-buttons-in-wpf) – Ral Zarek Jul 14 '14 at 10:09

1 Answers1

39

If you want radio buttons that look like toggle buttons won't radio buttons styled to look like toggle buttons solve your problem?

<StackPanel>
    <RadioButton GroupName="groupFoo" Style="{StaticResource {x:Type ToggleButton}}">Button 1</RadioButton>
    <RadioButton GroupName="groupFoo" Style="{StaticResource {x:Type ToggleButton}}">Button 2</RadioButton>
</StackPanel>
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • yeah i think that actually solved my problem. This is so much easier than i tried to do it. Even though I thought about it a short moment, i couldn't imagine it works that well and that easy. I will edit the solution into the question above. Thanks a lot! – Sören Feb 08 '11 at 15:26
  • 1
    @Sören: That's one of the greatest things about WPF: behavior is separate from presentation. Also, it would be better to post the solution as an answer. After a while you can accept it as the correct answer. – R. Martinho Fernandes Feb 08 '11 at 15:28
  • i know, i tried to accept it and it did not work yet (3 min lock). Now it did. Thanks again! – Sören Feb 08 '11 at 15:34
  • 6
    the problem is that you cant uncheck – GorillaApe Dec 10 '12 at 16:17
  • Unfortunately "x:Type ToggleButton" trick doesn't work anymore - I have VS 15.7.4 and .NET 4.7.2 and VS complains "resource "{x:Type ToggleButton}" could not be resolved"! Doh... – Vincent Jul 17 '18 at 12:34