Horizontal RadioButtons in a ListBox

I have a hard time making it work. The code is in the MVVM application, where I bind the switches to a property in the ViewModel.

I am trying to implement this SO answer

Everything works fine, except that the buttons are arranged vertically. Now this seems like an easy solution, just change the ItemPanelTemplate.

Here is my code:

<ListBox ItemsSource="{Binding ItemOptions}" SelectedItem="{Binding SelectedOption}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" IsItemsHost="True" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}" > <RadioButton Content="{TemplateBinding Content}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> </ListBox> 

However, the elements are kept upright. Any ideas why this does not affect the orientation of the ListBox? What am I missing?

Thanks Jerry

+4
source share
1 answer

Try the following:

  <ListBox.Template> <ControlTemplate TargetType="{x:Type ListBox}"> <ScrollViewer x:Name="scrollviewer" HorizontalScrollBarVisibility="Visible" CanContentScroll="False"> <StackPanel IsItemsHost="True" Orientation="Horizontal" /> </ScrollViewer> </ControlTemplate> </ListBox.Template> 

I tried to get this to work with ItemsPanelTemplate, like you, without success. This worked great for me, hope it helps too!

Hello

+2
source

All Articles