选择行与该行关联的数据后,WPF Datagrid会执行操作

时间:2019-06-27 17:12:49

标签: c# wpf

获得当前正在通过数据网格显示的对象列表。我需要做的是,当用户双击指定的行时,它将打开另一个窗口,该窗口是一个模型,显示与当前所选行的对象关联的数据。我不知道如何a)检测用户双击的时间,以及b)如何知道用户双击的行。

XAML:

    <DataGrid x:Name="BlueprintsDataGrid" Grid.Row="0" IsReadOnly="True" 
              ItemsSource="{Binding Path=Game.Blueprints, ElementName=uc}" 
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Width="80*" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="ME" Width="30*" Binding="{Binding MaterialEfficiency}"
                                HeaderStyle="{StaticResource HeaderRightJustify}"
                                CellStyle="{StaticResource ColumnRight}"/>
            <DataGridCheckBoxColumn Header="BPO" Width="30*" Binding="{Binding IsOrginial}"
                                    HeaderStyle="{StaticResource HeaderRightJustify}"/>
        </DataGrid.Columns>
    </DataGrid>

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

<DataGrid x:Name="BlueprintsDataGrid" Grid.Row="0" IsReadOnly="True" 
              ItemsSource="{Binding Path=Game.Blueprints, ElementName=uc}" 
              SelectedItem="{Binding CurrentSelection}"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Width="80*" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="ME" Width="30*" Binding="{Binding MaterialEfficiency}"
                                HeaderStyle="{StaticResource HeaderRightJustify}"
                                CellStyle="{StaticResource ColumnRight}"/>
            <DataGridCheckBoxColumn Header="BPO" Width="30*" Binding="{Binding IsOrginial}"
                                    HeaderStyle="{StaticResource HeaderRightJustify}"/>
        </DataGrid.Columns>
    </DataGrid>

注意 CurrentSelection 必须存在于ViewModel类中。此属性必须实现INotifyPropertyChanged接口,并且必须是 ItemsSource 持有的对象类型!

要识别鼠标双击,可以将上述xaml扩展为类似

 <DataGrid x:Name="BlueprintsDataGrid" Grid.Row="0" IsReadOnly="True" 
              ItemsSource="{Binding Path=Game.Blueprints, ElementName=uc}" 
              SelectedItem="{Binding CurrentSelection}"
              AutoGenerateColumns="False">
 <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction Command="{Binding DoubleClickedCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Width="80*" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="ME" Width="30*" Binding="{Binding MaterialEfficiency}"
                                HeaderStyle="{StaticResource HeaderRightJustify}"
                                CellStyle="{StaticResource ColumnRight}"/>
            <DataGridCheckBoxColumn Header="BPO" Width="30*" Binding="{Binding IsOrginial}"
                                    HeaderStyle="{StaticResource HeaderRightJustify}"/>
        </DataGrid.Columns>
    </DataGrid>

注意:因此,您在ViewModel中需要ICommand类型的属性DoubleClickedCommand。在那里,您可以检查您的CurrentSelection。

public ICommand DoubleClickedCommand { get; set; }

在您的ViewModel构造函数中:

DoubleClickedCommand = new RelayCommand(DoubleClick);

方法本身:

private void DoubleClick()
{
    if(CurrentSelection == null) { return; }
    // Do stuff in here            
}

答案 1 :(得分:0)

您可以处理MouseDoubleClick事件并投射事件源的DataContext

private void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    var fe = e.OriginalSource as FrameworkElement;
    if (fe != null)
    {
        var blueprint = fe.DataContext as BluePrint;
        if (blueprint != null)
        {
            //open window...
        }
    }
}

XAML:

<DataGrid x:Name="BlueprintsDataGrid" Grid.Row="0" IsReadOnly="True" 
          ItemsSource="{Binding Path=Game.Blueprints, ElementName=uc}" 
          AutoGenerateColumns="False"
          MouseDoubleClick="Dgm_MouseDoubleClick">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Width="80*" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="ME" Width="30*" Binding="{Binding MaterialEfficiency}"
                            HeaderStyle="{StaticResource HeaderRightJustify}"
                            CellStyle="{StaticResource ColumnRight}"/>
        <DataGridCheckBoxColumn Header="BPO" Width="30*" Binding="{Binding IsOrginial}"
                                HeaderStyle="{StaticResource HeaderRightJustify}"/>
    </DataGrid.Columns>
</DataGrid>

将BluePrint更改为您的班级名称。