mardi 12 octobre 2021

Binding Checkbox click event inside ItemsControl to viewmodel

I have an ItemsControl that creates checkboxes based on DigitalFilterSubsystems. I want to trigger an event handler on the viewmodel whenever a checkbox is clicked. At them moment my 'Click' property of the CheckBox is complaining that 'Checked_Triggered' is expecting a property. Could someone explain what I am doing wrong and show a solution?

<ItemsControl Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" ItemsSource="{Binding DigitalFilterSubsystems}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <UniformGrid />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    
                                    <ItemsControl.ItemTemplate>
                                        
                                        <DataTemplate>
                                            <StackPanel Orientation="Vertical">
                                                <TextBlock HorizontalAlignment="Center" Text="{Binding }" Foreground="Black" FontSize="12"/>
                                                <CheckBox HorizontalAlignment="Center" Click="{Binding Path=Checked_Triggered, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type vm:SimulatorControlViewModel}}, Mode=TwoWay}"/>   
                                            </StackPanel>
                                        </DataTemplate>
                                        
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>

In my viewmodel called SimulatorControlViewModel:

public void Checked_Triggered(object sender, RoutedEventArgs e)
        { 
            
        }



Aucun commentaire:

Enregistrer un commentaire