4

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>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Mike
  • 41
  • 1
  • 3

2 Answers2

7

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...

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 1
    Thanks for the suggestion, but I have multiple ToggleButtons on the screen...and they could be variable number of them. – Mike Nov 22 '09 at 03:11
  • How does having multiple buttons prevent you binding them to the viewmodel? They obviously have some unique identifier so that could be used as a key to a collection property. – Andy Dent Nov 24 '09 at 14:46
  • 1
    Each ToggleButton corresponds to an item in the Modifiers collection, so you must put the IsChecked property in the collection items. – Thomas Levesque Nov 24 '09 at 17:51
1

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?

Andrew
  • 26,629
  • 5
  • 63
  • 86
  • Hmm...not exactly sure what you're suggesting here. Can you expand? – Mike Nov 22 '09 at 03:13
  • http://msdn.microsoft.com/en-us/library/ms752308.aspx gives an overview of commanding, ICommandSource interface gives you the CommandParameter, but you may find implementing RoutedCommand (and getting RoutedEventArgs) easier / more suitable. – Andrew Nov 22 '09 at 13:42