Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ToggleButton and DelegateCommand

Is there a way to determine if a ToggleButton is Checked/Unchecked via DelegateCommands?

TIA, mike

XAML code below. I'm using ItemsControl and binding to a collection. I'm basically wanting a way to get the toggle status of each button when it's clicked.

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <ItemsControl ItemsSource="{Binding Modifiers, Mode=TwoWay}">
        <ItemsControl.Template>
            <ControlTemplate>
                <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto">
                    <WrapPanel Margin="10" Width="{TemplateBinding Width}"
                               Height="{TemplateBinding Height}" 
                               FlowDirection="LeftToRight" IsItemsHost="true">
                    </WrapPanel>
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ToggleButton FontSize="18" Opacity="0.8"
                              Command="{Binding DataContext.ModifierToggleCommand, 
                                        RelativeSource={RelativeSource FindAncestor,
                                        AncestorType={x:Type Views:ModifiersView}}}" 
                              CommandParameter="{Binding}" Height="80" Width="200" Margin="5"
                              Content="{Binding Path=ModifierName}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>
like image 661
Mike Avatar asked Feb 28 '23 00:02

Mike


2 Answers

A simpler solution would be to bind the IsChecked property to a property of your ViewModel. That way you just have to check the property value...

like image 101
Thomas Levesque Avatar answered Mar 08 '23 02:03

Thomas Levesque


Could you specify the CommandParameter declaratively in the XAML and use an element binding to populate the value with the current value of the toggle?

like image 23
Andrew Avatar answered Mar 08 '23 01:03

Andrew