简体   繁体   English

DataGrid中ComboBoxItem的绑定错误

[英]Binding-Error with ComboBoxItem in DataGrid

I have a UserControl with a DataGrid in it. 我有一个带有DataGridUserControl The columns of the DataGrid do I create as DataGridTemplateColumn s. 我将DataGrid的列创建为DataGridTemplateColumn The DataGrid looks something like: DataGrid看起来像:

<DataGrid Grid.Row="1" Style="{StaticResource DataGridStyle}" Margin="5"
      ItemsSource="{Binding Storage.Items, UpdateSourceTrigger=PropertyChanged}"
      SelectedItem="{Binding SelectedItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
      MaxHeight="400" VerticalAlignment="Top">
      <DataGrid.Columns>
        <DataGridTemplateColumn Header="Comment" Width="*" SortMemberPath="Comment">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Comment}" Margin="3,2" VerticalAlignment="Center"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        // several more column-definitions                      

        <DataGridTemplateColumn Header="Actions" Width="90">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Margin="5" Content="Action..." Cursor="Hand" >
                        <Button.Template>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <TextBlock TextDecorations="Underline" HorizontalAlignment="Center" Foreground="Blue">
                                    <ContentPresenter/>
                                </TextBlock>
                            </ControlTemplate>
                        </Button.Template>
                        <Button.Style>
                            <Style TargetType="{x:Type Button}">
                                <Style.Triggers>
                                    <EventTrigger RoutedEvent="Click">
                                        <EventTrigger.Actions>
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                                        <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                                    </BooleanAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger.Actions>
                                    </EventTrigger>
                                </Style.Triggers>
                                <Setter Property="ContextMenu">
                                    <Setter.Value>
                                        <ContextMenu>                                                            
                                            <MenuItem Header="Edit"/>
                                            <MenuItem Header="Delete" Command="{Binding DataContext.CommonCommand, 
                                                RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" 
                                                      CommandParameter="{x:Static defs:CommonCommandTarget.Delete}"/>
                                            <Separator/>
                                        </ContextMenu>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Button.Style>
                    </Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>+
    </DataGrid.Columns>
</DataGrid>

My problem is the binding of the Delete-Command of the ContextMenu in the last column. 我的问题是在最后一列中绑定了ContextMenu的Delete-Command。 The contextmenu is shown but on a click nothing happens. 显示上下文菜单但单击时没有任何反应。

In the output I can see a binding-error: 在输出中我可以看到绑定错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. System.Windows.Data错误:4:无法找到绑定源,引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.DataGrid',AncestorLevel ='1''。 BindingExpression:Path=DataContext.CommonCommand; BindingExpression:路径= DataContext.CommonCommand; DataItem=null; 的DataItem = NULL; target element is 'MenuItem' (Name=''); target元素是'MenuItem'(Name =''); target property is 'Command' (type 'ICommand') target属性是'Command'(类型'ICommand')

Outside the DataGrid or a Button directly in the Template-Column just works fine with the binding. DataGrid之外或直接在Template-Column中的Button可以正常使用绑定。 so the Command is correctly. 所以Command是正确的。

What Ancestor or RelativeSource or whatever do I have to provide that my Command in the ViewModel will be called? 什么AncestorRelativeSource或我必须提供什么,我将调用ViewModel中的命令?

You can only use the RelativeSource markup extension from within the scope of a Visual Tree. 您只能在Visual Tree的范围内使用RelativeSource标记扩展。 DataGridTemplateColumn is not a part of the Visual Tree. DataGridTemplateColumn不是Visual Tree的一部分。 Check out this answer . 看看这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM