Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting row information after a doubleclick

I am trying to retrieve row info from a datagrid after a double click event. I have the event setup, but now I just need to setup the function to retrieve the data from the row.

XAML:

    <DataGrid 
        Width="Auto" 
        SelectionMode="Extended" 
        IsReadOnly="True" 
        Name="ListDataGrid"
        AutoGenerateColumns="False"
        ItemsSource="{Binding ListFieldObject.MoviesList}"
        DataContext="{StaticResource MovieAppViewModel}"
        cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect()]">

        <DataGrid.Columns>
            <DataGridTextColumn Width="200" IsReadOnly="True" Header="Title" Binding="{Binding Title}"/>
            <DataGridTextColumn Width="100" IsReadOnly="True" Header="Rating" Binding="{Binding Rating}"/>
            <DataGridTextColumn Width="100" IsReadOnly="True" Header="Stars" Binding="{Binding Stars}"/>
            <DataGridTextColumn Width="93" IsReadOnly="True" Header="Release Year" Binding="{Binding ReleaseYear}"/>
        </DataGrid.Columns>
    </DataGrid>

C# (MVVM ViewModel):

     public void RowSelect()
     {
         //now how to access the selected row after the double click event?
     }

Thanks Much!

like image 771
Josh Avatar asked Feb 28 '12 17:02

Josh


4 Answers

You can alternatively do this:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="cal:Message.Attach" Value="[MouseDoubleClick] = [Action RowSelect($dataContext)]"/>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

Then

public void RowSelect(MoviesListItem movie)
{
     //now how to access the selected row after the double click event?
}
like image 95
David Kiff Avatar answered Oct 12 '22 01:10

David Kiff


You can just pass $dataContext on your XAML:

 cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect($dataContext)]">

And change your method to:

public void RowSelect(MoviesListItem movie)
{
     //now how to access the selected row after the double click event?
}

//EDIT Sorry, the above solution will work only if the action is on the datatemplate itself... another solution would be to have a SelectedItem bind and just use it on your method:

<DataGrid 
    SelectedItem="{Binding SelectedMovie,Mode=TwoWay}"
    cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect()]">

and on your code:

public void RowSelect()
{
   //SelectedMovie is the item where the user double-cliked
}
like image 29
Leo Avatar answered Oct 12 '22 01:10

Leo


(hope it will help) I am not sure about your case, but this is what I do in winforms:

            int index = dataGridView2.CurrentRow.Index; //determine which item is selected
            textBox8.Text = dataGridView2.Rows[index].Cells[0].Value.ToString(); //add login
like image 26
Andrew Avatar answered Oct 12 '22 02:10

Andrew


You can do this by modifying the control template for the DataGridRows exposed by the DataGrid. The example below uses WPF and the Aero theme.

The only thing I've done is removed your previous cal:Message.Attach call and move it to a new "placeholder" ContentControl that surrounds the Border (x:Name=DGR_Border) in the "default" control template. (I used ContentControl because it has no visuals of its own and it exposes a MouseDoubleClick event.)

<DataGrid Width="Auto" 
          SelectionMode="Extended" 
          IsReadOnly="True" 
          Name="ListDataGrid"
          AutoGenerateColumns="False"
          ItemsSource="{Binding ListFieldObject.MoviesList}"
          DataContext="{StaticResource MovieAppViewModel}">

    <DataGrid.Columns>
        <DataGridTextColumn Width="200" IsReadOnly="True" Header="Title" Binding="{Binding Title}"/>
        <DataGridTextColumn Width="100" IsReadOnly="True" Header="Rating" Binding="{Binding Rating}"/>
        <DataGridTextColumn Width="100" IsReadOnly="True" Header="Stars" Binding="{Binding Stars}"/>
        <DataGridTextColumn Width="93" IsReadOnly="True" Header="Release Year" Binding="{Binding ReleaseYear}"/>
    </DataGrid.Columns>
    <DataGrid.RowStyle>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>                                      
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <ContentControl cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect($datacontext)]">
                        <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <SelectiveScrollingGrid>
                                <SelectiveScrollingGrid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </SelectiveScrollingGrid.ColumnDefinitions>
                                <SelectiveScrollingGrid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </SelectiveScrollingGrid.RowDefinitions>
                                <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                                <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                            </SelectiveScrollingGrid>
                        </Border>
                    </ContentControl>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </DataGrid.RowStyle>
</DataGrid>

The only other thing you'll have to do is modify your RowSelect() method to accept a parameter of whatever type you're using here (I just assumed it was a 'Movie' type).

public void RowSelect(Movie movie)
{
   // do something with 'movie'
}
like image 43
Adrian Avatar answered Oct 12 '22 00:10

Adrian