Button inside DataGrid wpf MVVM

I am following MVVM in this project.

I have a datagrid WPF,

ItemsSource is (ItemsSource="{Binding Documents}")tied to ObservableCollection<Document>,

SelectedItem is (SelectedItem="{Binding CurrentDocument, Mode=TwoWay}")attached to WorkQueueDocument,

I also used interaction triggers to double-click to load the selected document into a new WIndow file.

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

I have defined / linked the columns of my datagrid with the corresponding attributes of the WorkQueueDocument class.

<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 properties defined here -->
</DataGrid.ColumnHeaderStyle>

I have to load the document when the user selects a row (document) in the grid - for this property, CurrentDocument is defined as follows:

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

<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 , () ( ) **

googled - , , . , .

, , .

P.S: 1. WPF,.Net 4.0 MVVM

  1. , . [ ]
+4
1
  • delete

    CommandParameter="{Binding}"

  • , - ,

    yourDocumentObservableCollection.Remove(CommandParameter)

, .

+3

All Articles