Arsen Zahray
Arsen Zahray

Reputation: 25367

Button.Command not working

I've got XAML file with following structure:

<UserControl>
    <Grid>
        ...
        <ListBox>
            <ListBox.ItemTemplate>              
                <DataTemplate>
                    <Expander>
                        <ListBox>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Expander>
                                        <StackPanel>
                                            <kb:Resizer>
                                                <DataGrid>  
                                                    <DataGrid.Columns>  
                                                        ...
                                                            <DataGridTemplateColumn IsReadOnly="True">
                                                                <DataGridTemplateColumn.CellTemplate>
                                                                    <DataTemplate>
                                                                        <Button Style="{StaticResource ChromelessButton}">
                                                                            <Button.CommandParameter>
                                                                                <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                                                                                     <Binding />
                                                                                     <Binding Path="DataContext.Items" RelativeSource="{RelativeSource AncestorType=DataGrid}"/>
                                                                                </MultiBinding>
                                                                            </Button.CommandParameter>
                                                                            <Button.Content>
                                                                                <Image Source="./../Images/close.png" Width="15"></Image>
                                                                            </Button.Content>
                                                                            <Button.Command>                                                            
                                                                                <MultiBinding Converter="{StaticResource TriggerConverter}">
                                                                                    <Binding Path="DataContext.DeleteCommand" 
                                                                                        RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyClass}}" />
                                                                                    <Binding />
                                                                                </MultiBinding> 
                                                                            </Button.Command>
                                                                        </Button>
                                                                    </DataTemplate>                                             
                                                                </DataGridTemplateColumn.CellTemplate>                                          
                                                            </DataGridTemplateColumn>
                                                        </DataGrid.Columns>  
                                                    </DataGrid> 
                                                </kb:Resizer>                                       
                                            </StackPanel>
                                        </Expander>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Expander>                 
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

the program compiles and executes properly, and I'm getting no error messages in the output, but DeleteCommand never gets hit.

What am I doing wrong?

Upvotes: 0

Views: 333

Answers (1)

akjoshi
akjoshi

Reputation: 15802

<Button.Command>                                                            
  <MultiBinding Converter="{StaticResource TriggerConverter}">
     <Binding Path="DataContext.DeleteCommand" 
        RelativeSource="{RelativeSource Mode=FindAncestor, 
                AncestorType={x:Type local:MyClass}}" />
     <Binding />
  </MultiBinding> 
</Button.Command>

This xaml in your code looks problematic...

  • When you have DeleteCommand in DataContext then what's the need of TriggerConverter?
  • This AncestorType={x:Type local:MyClass}}" looks wrong; is it pointing to some control or your class? I don't see this control(of type MyClass) in your control hierarchy. Check this on how RelativeSource works.

Upvotes: 1

Related Questions