c# - DataGrid wpf MVVM 内的按钮

标签 c# wpf events mvvm datagrid

我在这个项目中遵循 MVVM。

我有 WPF 数据网格,

元素来源 (ItemsSource="{Binding Documents}")绑定(bind)到 ObservableCollection<Document> ,

已选项目 (SelectedItem="{Binding CurrentDocument, Mode=TwoWay}")绑定(bind)到 WorkQueueDocument ,

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

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
        <i:InvokeCommandAction Command="{Binding ShowViewerCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

我已经将我的数据网格的列定义/绑定(bind)到 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>

我应该在用户选择网格中的行(文档)时加载文档 - 因为 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;
            }
        }

    }
}

现在,问题是 - 我想向此数据网格添加一个删除按钮列,这样当用户按下删除按钮时 - 文档会直接删除而不加载文档。 我将以下 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>

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

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

ItemsSource="{Binding Documents}"

第二个命令在上面数据网格的列中的删除按钮上

<Button Name="Delete" 
Content="Delete" 
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteCommand}" 
CommandParameter="Delete">

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

DeleteCommand(理想情况下应该如此)。但是,如果我删除此“SelectedItem”属性 - deleteCommand 将被触发(但我不会获得选定的行)。

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

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

我知道了 link ,但不确定这是否是唯一的方法。

附言: 1. 我正在使用 WPF、.Net 4.0 和 MVVM

  1. 请勿推荐第三方解决方案。 [除非唯一的选择]

最佳答案

  • 删除命令参数应该就是

    CommandParameter="{Binding}"

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

    yourDocumentObservableCollection.Remove(CommandParameter)

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

关于c# - DataGrid wpf MVVM 内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858692/

相关文章:

c# - 如何不打开相同的 Popup WPF MVVM?

c# - 在 WPF 中创建 'wipe' 动画

javascript - 向下滚动触发动画

events - GWT 中的 Fire ResizeEvent (Google Web Toolkit)

c# - 可以用 C# 中的用户输入计算年龄,但不知道为什么会这样

c# - 无法从 ASP.NET 中的目录查看最新文件

c# - 声明具有复杂类型约束的继承泛型

c# - 在 ASP.NET MVC 中以编程方式验证模型列表

c# - Winforms 中的事件和 WPF 中的命令有什么区别?

javascript - Javascript 是否有类似于 VBA 的 DoEvents 的东西?