0

我有具有以下结构的 XAML 文件:

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

该程序可以正确编译和执行,并且我在输出中没有收到任何错误消息,但DeleteCommand从未受到打击。

我究竟做错了什么?

4

1 回答 1

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

您代码中的这个 xaml 看起来有问题......

  • 当你有DeleteCommand那么DataContext什么是需要的TriggerConverter
  • AncestorType={x:Type local:MyClass}}"看起来不对;它是指向某些控件还是您的班级?我MyClass在您的控件层次结构中看不到此控件(类型为 )。检查一下RelativeSource是如何工作的。
于 2012-06-24T06:52:56.377 回答