繁体   English   中英

DataGrid中的按钮wpf MVVM

[英]Button inside DataGrid wpf 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>

我已将datagrid的列定义/绑定到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没有被解雇。 我试图弄明白为什么并发现我有

第一个命令在datagrid中,用于在选择行时加载文档

ItemsSource="{Binding Documents}"

第二个命令是在上面的datagrid的coluumn中的删除按钮上

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

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

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

另外(在调试时我注意到)当我们第二次按下(点击)时,执行** DeleteCommand (现在已经选择了行)** **

我用谷歌搜索 - 并找到了一些可能的解决方案,如优先绑定和隧道,但无法实现。 请指导我完成这个。

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

PS:1。我正在使用WPF,.Net 4.0和MVVM

  1. 请不要建议第三方解决方案。 [除非唯一的选择]
  • delete命令参数应该只是

    CommandParameter="{Binding}"

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

    yourDocumentObservableCollection.Remove(CommandParameter)

通过这种方式,您无需关心文档是否重点关注。

暂无
暂无

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

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