DataGrid wpf MVVM 内的按钮

Bhr*_*mar 6 c# wpf events datagrid mvvm

我在这个项目中关注 MVVM。

我有 WPF 数据网格,

ItemsSource(ItemsSource="{Binding Documents}")绑定到ObservableCollection<Document>,

SelectedItem(SelectedItem="{Binding CurrentDocument, Mode=TwoWay}")绑定到WorkQueueDocument,

我还使用交互触发器来捕获鼠标的双击 - 以便在新窗口中加载选定的文档。

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
        <i:InvokeCommandAction Command="{Binding ShowViewerCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

我已将数据网格的列定义/绑定到 WorkQueueDocument 类的相应属性。

<DataGrid.Columns>
    <DataGridTextColumn Width="Auto"
                            MinWidth="100"
                            Header="Name"                                                            
                            Binding="{Binding Name}">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Margin" Value="2,0,0,0" />
                <Setter Property="ToolTip" Value="{Binding Name}" />
            </Style>
        </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>

    <!-- Many Other Columns Here... -->
</DataGrid.Columns>

<DataGrid.ColumnHeaderStyle>
        <!-- I have various designer style's properties defined here -->
</DataGrid.ColumnHeaderStyle>
Run Code Online (Sandbox Code Playgroud)

我应该在用户选择网格中的行(文档)时加载文档 - 对于该 CurrentDocument 属性定义如下:

public WorkQueueDocument CurrentDocument
{
    get
    {
        return this.currentDocument;
    }
    set
    {

        if (this.currentDocument != value)
        {
            this.currentDocument = value;
            this.OnPropertyChanged("CurrentDocument");
            this.IsDocumentSelected = true;

    // If we are in progress already, don't do anything
            if (!IsLoading && this.currentDocument != null)
            {
                IsLoading = true;
                LoadDocumentBackgroundWorker();// loading documenting async
            }


            if (this.currentDocument == null)
            {
                this.IsDocumentSelected = false;
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

现在,问题是 - 我想向这个数据网格添加一个删除按钮列,这样当用户按下删除按钮时 - 文档被直接删除而不加载文档。我将以下 xaml 添加到<DataGrid.Columns>

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <Button Name="DeleteBatch" 
            Content="Delete"
            Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteCommand}"
            CommandParameter="Delete"/>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Run Code Online (Sandbox Code Playgroud)

DeleteCommand不会被解雇。我试图找出原因并发现我有

第一个命令在数据网格中,用于在选择行时加载文档

ItemsSource="{Binding Documents}"
Run Code Online (Sandbox Code Playgroud)

第二个命令位于删除按钮上,该按钮位于上述数据网格的列中

<Button Name="Delete" 
Content="Delete" 
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteCommand}" 
CommandParameter="Delete">
Run Code Online (Sandbox Code Playgroud)

,我一次只能访问一个命令。当我单击按钮时 - 行(“显然”)被选中并执行“ SelectedItem ”的关联绑定,但没有跟进调用

DeleteCommand(理想情况下应该)。但是如果我删除这个“ SelectedItem ”属性 - deleteCommand 被触发(但我没有得到选定的行)。

另外(在调试时我注意到)**DeleteCommand在我们第二次按下(单击)时被执行(因为现在该行已经被选中)**

我用谷歌搜索 - 并发现了一些可能的解决方案,例如 Priority Binding 和 Tunneling ,但无法实施。请指导我完成这个。

我得到了这个链接,但不确定这是否是唯一的方法。

PS:1.我使用的是 WPF、.Net 4.0 和 MVVM

  1. 请不要建议第三方解决方案。【除非唯一的选择】

FPG*_*PGA 5

  • 删除命令参数应该只是

    CommandParameter="{Binding}"

  • 这意味着命令参数是文档引用本身,因此您可以在命令中执行以下操作

    yourDocumentObservableCollection.Remove(CommandParameter)

这样你就不需要关心文档是否有焦点。