1
votes

I have ToggleButtons that are dynamically created based on my datasource. I would like to have only one togglebutton checked at a time when a user clicks one. How can I accomplish this?

<UserControl.Resources>

    <ItemsPanelTemplate x:Key="HorizontalMiniDrawerList">
        <StackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>

    <DataTemplate x:Key="MiniDrawerRowTemplate">
        <ToggleButton x:Name="_MiniDrawerButton" Width="60" Height="85" Style="{DynamicResource MiniDrawerButtonWhite}" Checked="_MiniDrawerButton_Checked" >
        </ToggleButton>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Width}" Value="3">
                <Setter TargetName="_MiniDrawerButton" Property="Width" Value="185"/>
            </DataTrigger>                                

        </DataTemplate.Triggers> 
    </DataTemplate>

    <DataTemplate x:Key="MiniDrawerListItemTemplate">
        <ListBox SelectionMode="Multiple" Background="#00000000" BorderThickness="0" Width="500"
            ItemsPanel="{StaticResource HorizontalMiniDrawerList}"
            ItemTemplate="{StaticResource MiniDrawerRowTemplate}" 
            ItemsSource="{Binding Row}" >

        </ListBox>
    </DataTemplate>        

</UserControl.Resources>

<Grid Background="{DynamicResource ListBackgroundColor}" >

    <ListBox x:Name="_MiniDrawerRows" BorderThickness="0" Background="Transparent"  Margin="107,84,225,217" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ItemsSource="{Binding Path=MiniDrawerRows, diagnostics:PresentationTraceSources.TraceLevel=High}" 
             ItemTemplate="{StaticResource MiniDrawerListItemTemplate}"  >                  
    </ListBox>      

</Grid>

Update: Instead of using a togglebutton I used a radiobutton and changed the style of the radio button to look like a togglebutton.

<Style x:Key="MiniDrawerButtonWhiteRadioToToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}">
1

1 Answers

0
votes

I assume you mean "only one" instead of "only when". In that case you can use RadioButton (which is derived from ToggleButton) instead and set a GroupName on _MiniDrawerButton in your ItemTemplate. It looks like you are probably already using a custom ControlTemplate so you can use the same one for RadioButton by just changing the Style and ControlTemplate TargetTypes.