Resolved Dynamically added radio buttons in itemscontrol style issue

Tewl

Member
Joined
Jan 13, 2022
Messages
8
Programming Experience
5-10
So on my app I have a menubar that uses radio buttons to set the current View. I have 2 radio buttons and an itemscontrol that is bound to a collection that populates with another radio button when a document is created.
My issue is that the radio buttons in the items control background changes when clicked but do not revert back when I click to another. The two radio buttons not in the itemscontrol work fine. All of are bound to the same style.


MainWindow:
            <RadioButton Grid.Row="0" Content="../Icons/Home.png"
                         Height="50"
                         IsChecked="True"
                         Style="{StaticResource MenuButtonTheme}"
                         Command="{Binding HomeViewCommand}"/>
            <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Hidden">
                <ItemsControl Grid.Row="1" ItemsSource="{Binding Channels}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <RadioButton Height="50" Content="../Icons/Document.png"
                                     Style="{StaticResource MenuButtonTheme}"
                                     Command="{Binding DataContext.DocumentViewCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                     CommandParameter="{Binding DocumentID}"
                                     ToolTip="{Binding DocumentName}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
            <RadioButton Grid.Row="2" Content="../Icons/Settings.png"
                         Height="50"
                         Style="{StaticResource MenuButtonTheme}"
                         Command="{Binding SettingsViewCommand}"/>


MenuButtonTheme:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style BasedOn="{StaticResource {x:Type ToggleButton}}"
           TargetType="{x:Type RadioButton}"
           x:Key="MenuButtonTheme">
        <Style.Setters>
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Margin" Value="0,0,0,0"/>
            <Setter Property="Padding" Value="0,0,0,0"/>
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <Grid VerticalAlignment="{TemplateBinding VerticalAlignment}"
                              HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                              Background="{TemplateBinding Background}">
                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    Background="Transparent">
                                <Image Width="24"
                                       Height="24"
                                       VerticalAlignment="Center"
                                       Source="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Background" Value="#3A3B3C" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</ResourceDictionary>

Any help is appreciated, thanks!
 
After bit of searching through StackOverflow I found a solution which was setting a groupname property for each radio button. :sneaky:
 
Back
Top Bottom