简体   繁体   English

如何在WPF中获取DevExpress GridControl的选定行值?

[英]How to get the selected row values of DevExpress GridControl in WPF?

I asked this question in Oct, 2012 when I was working on windows application. 当我在Windows应用程序上工作时,我在2012年10月问了这个问题。 now when I turn to WPF application, I get the same problem again ie How to get the selected row values of DevExpress GridControl in WPF ? 现在当我转向WPF应用程序时,我再次遇到相同的问题,即如何在WPF中获取DevExpress GridControl的选定行值? I've failed to find my answer on google and none of the answers in the above mentioned link is working. 我在Google上找不到我的答案,上述链接中的所有答案均无效。 there is nothing like CellClick, RowClick or RowCellClick event in devexpress gridcontrol of wpf as it is in winform gridcontrol. wpf的devexpress gridcontrol中没有像WinClick gridcontrol中那样的CellClick,RowClick或RowCellClick事件。 I'll be glad if someone can solve out this problem 如果有人能解决这个问题我会很高兴

Edit 编辑

I have updated my application with required namespaces as you updated in your answer but the problem remains the same. 当您在答案中更新时,我已使用所需的名称空间更新了我的应用程序,但问题仍然相同。 i'm getting following tow errors in 我正在关注以下拖曳错误

<Grid.DataContext>
        <dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>
    </Grid.DataContext>

1- the type 'dxmvvm:ViewModelSource'" was not found in dxmvvm:ViewModelSource 1-在dxmvvm:ViewModelSource中找不到类型'dxmvvm:ViewModelSource'“

2- name "EntitiesViewModel" does not exist in the namespace "clr-namespace:DXApplication1" 2-名称“ EntitiesViewModel”在名称空间“ clr-namespace:DXApplication1”中不存在

my application coding is as follows 我的应用程序编码如下

XAML XAML

<dx:DXWindow
    x:Class="DXApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DXApplication1"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    Title="DXApplication" Height="700" Width="1100"
    SnapsToDevicePixels="True" UseLayoutRounding="True">

    <dx:DXWindow.Resources>
    </dx:DXWindow.Resources>

    <Grid Margin="12">
        <Grid.DataContext>
            <dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>
        </Grid.DataContext>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Margin="0,0,0,8">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="8"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Item1" Grid.Row="0" VerticalAlignment="Center"/>
            <Label Content="Item2" Grid.Row="1" VerticalAlignment="Center"/>
            <Label Content="Item3" Grid.Row="2" VerticalAlignment="Center"/>
            <dxe:TextEdit Text="{Binding SelectedEntity.Item1}" Grid.Row="0" Grid.Column="2"/>
            <dxe:TextEdit Text="{Binding SelectedEntity.Item2}" Grid.Row="1" Grid.Column="2"/>
            <dxe:TextEdit Text="{Binding SelectedEntity.Item3}" Grid.Row="2" Grid.Column="2"/>
        </Grid>
        <dxg:GridControl Grid.Row="1" 
                     AutoGenerateColumns="None" 
                     ItemsSource="{Binding Entities}"
                     SelectedItem="{Binding SelectedEntity}">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="Item1"/>
                <dxg:GridColumn FieldName="Item2"/>
                <dxg:GridColumn FieldName="Item3"/>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView ShowTotalSummary="True" AllowEditing="False"/>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</dx:DXWindow>

EntitiesViewModel EntitiesViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace DXApplication1
{
    public class EntitiesViewModel
    {
        public EntitiesViewModel()
        {
            LoadEntities();
        }
        void LoadEntities()
        {
            Entities = new ObservableCollection<Entity>
        {
            new Entity(){ Item1="A", Item2="A0", Item3="A00"},
            new Entity(){ Item1="B", Item2="B0", Item3="B00"},
            new Entity(){ Item1="C", Item2="C0", Item3="C00"},
        };
        }
        public ObservableCollection<Entity> Entities { get; private set; }
        public virtual Entity SelectedEntity { get; set; } // Bindable property
    }
    public class Entity
    {
        public string Item1 { get; set; }
        public string Item2 { get; set; }
        public string Item3 { get; set; }
    }
}

Image for libraries, error, class etc 库,错误,类等的图像

在此处输入图片说明

You can use DataControlBase.CurrentItemChanged event. 您可以使用DataControlBase.CurrentItemChanged事件。
Here is example: 这是示例:
WPF: WPF:

<dxg:GridControl x:Name="gridControl1" ItemsSource="{Binding Data}" CurrentItemChanged="gridControl1_CurrentItemChanged">
</dxg:GridControl>

Event handler: 事件处理程序:

private void gridControl1_CurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
{
    TBGRNo.Text = gridControl1.GetFocusedRowCellValue("GRNo").ToString();
    TBSName.Text = gridControl1.GetFocusedRowCellValue("SName").ToString();
    TBFName.Text = gridControl1.GetFocusedRowCellValue("FName").ToString();    
}

Look at the selection topic in the documentation at the Devexpress Homepage: Grid Selection Topic 在Devexpress主页上的文档中查看选择主题网格选择主题

If you are using the MVVM pattern you should also have a look at the SelectedItems Property . 如果使用的是MVVM模式,则还应该查看SelectedItems属性 The example there shows how to bind to the selected items: 那里的示例显示了如何绑定到所选项目:

<dxg:GridControl ItemsSource="{Binding Source}" SelectedItems="{Binding Selection}">

I found the solution as follows 我发现解决方案如下

<Grid>
    <dxg:GridControl Name="gridcontrol" AutoGenerateColumns="AddNew" HorizontalAlignment="Left" Margin="23,139,0,0" VerticalAlignment="Top" Height="315" Width="575">
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="Item1"/>
            <dxg:GridColumn FieldName="Item2"/>
            <dxg:GridColumn FieldName="Item3"/>
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView Name="gridview" ShowTotalSummary="True" AllowEditing="False"/>
        </dxg:GridControl.View>
    </dxg:GridControl>
    <dxe:TextEdit Name="TBItem1" Text="{Binding SelectedItem.Item1, ElementName=gridcontrol, Mode=OneWay}" HorizontalAlignment="Left" Margin="124,10,0,0" VerticalAlignment="Top" Width="150"/>
    <dxe:TextEdit Name="TBItem2" Text="{Binding SelectedItem.Item2, ElementName=gridcontrol, Mode=OneWay}" HorizontalAlignment="Left" Margin="124,49,0,0" VerticalAlignment="Top" Width="150"/>
    <dxe:TextEdit Name="TBItem3" Text="{Binding SelectedItem.Item3, ElementName=gridcontrol, Mode=OneWay}" HorizontalAlignment="Left" Margin="124,84,0,0" VerticalAlignment="Top" Width="150"/>
    <Label Content="Item1" HorizontalAlignment="Left" Margin="61,10,0,0" VerticalAlignment="Top" Width="48"/>
    <Label Content="Item2" HorizontalAlignment="Left" Margin="61,49,0,0" VerticalAlignment="Top"/>
    <Label Content="Item3" HorizontalAlignment="Left" Margin="61,80,0,0" VerticalAlignment="Top"/>
</Grid>

As far as I can see you're want to display list of entities, then provide an UI for selecting one of these entitites (via gridcontrol) and edit the selected entity properties in separate view (via text editors). 据我所知,您要显示实体列表,然后提供一个用于选择这些实体之一的UI(通过gridcontrol)并在单独的视图中(通过文本编辑器)编辑选定的实体属性。

So, I suggest you to use the MVVM approach. 因此,我建议您使用MVVM方法。 And, since you are already using DevExpress controls, I suggest you use the DevExpress MVVM Framework to make your MVVM as easy as it possible. 并且,由于您已经在使用DevExpress控件,因此建议您使用DevExpress MVVM Framework使MVVM尽可能简单。

Step 1: Define a ViewModel class that contains your entities collection available via the Entities property (you can load these entities in way as it needed, from it needed and when it needed) and provide the SelectedEntity property: 步骤1:定义一个ViewModel类,该类包含可通过Entities属性使用的实体集合(您可以根据需要,从需要的方式以及在需要时的方式加载这些实体),并提供SelectedEntity属性:

public class EntitiesViewModel {
    public EntitiesViewModel() {
        LoadEntities();
    }
    void LoadEntities() {
        Entities = new ObservableCollection<Entity>
        {
            new Entity(){ Item1="A", Item2="A0", Item3="A00"},
            new Entity(){ Item1="B", Item2="B0", Item3="B00"},
            new Entity(){ Item1="C", Item2="C0", Item3="C00"},
        };
    }
    public ObservableCollection<Entity> Entities { get; private set; }
    public virtual Entity SelectedEntity { get; set; } // Bindable property
}
public class Entity {
    public string Item1 { get; set; }
    public string Item2 { get; set; }
    public string Item3 { get; set; }
}

Step 2: Define a View layout as follows: 步骤2:按如下方式定义视图布局:

...
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DXApplication1"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
...
<Grid Margin="12">
    <Grid.DataContext>
        <dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>
    </Grid.DataContext>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" Margin="0,0,0,8">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="8"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Content="Item1" Grid.Row="0" VerticalAlignment="Center"/>
        <Label Content="Item2" Grid.Row="1" VerticalAlignment="Center"/>
        <Label Content="Item3" Grid.Row="2" VerticalAlignment="Center"/>
        <dxe:TextEdit Text="{Binding SelectedEntity.Item1}" Grid.Row="0" Grid.Column="2"/>
        <dxe:TextEdit Text="{Binding SelectedEntity.Item2}" Grid.Row="1" Grid.Column="2"/>
        <dxe:TextEdit Text="{Binding SelectedEntity.Item3}" Grid.Row="2" Grid.Column="2"/>
    </Grid>
    <dxg:GridControl Grid.Row="1" 
                     AutoGenerateColumns="None" 
                     ItemsSource="{Binding Entities}"
                     SelectedItem="{Binding SelectedEntity}">
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="Item1"/>
            <dxg:GridColumn FieldName="Item2"/>
            <dxg:GridColumn FieldName="Item3"/>
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView ShowTotalSummary="True" AllowEditing="False"/>
        </dxg:GridControl.View>
    </dxg:GridControl>
</Grid>

*The main points-of-interest and tricks are: *主要的兴趣点和技巧是:
1) creating the EntitiesViewModel instance via the ViewModelSource. 1)通过ViewModelSource创建EntitiesViewModel实例。 This way allows you do not implement the INotifyPropertyChanged interface at the ViewModel's level - you can just declare the SelectedEntity property as virtual and delegate all the dirty-work to the POCO mechanism : 这种方式允许您不在ViewModel的级别上实现INotifyPropertyChanged接口-您可以仅将SelectedEntity属性声明为virtual并将所有脏工作委托给POCO机制

<dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>

2) all View' controls bindings are "looks" into the DataContext (which contains our ViewModel): 2)所有View控件的绑定都是“外观”到DataContext(包含我们的ViewModel)中:

...
Text="{Binding SelectedEntity.Item1}"
...
ItemsSource="{Binding Entities}"
SelectedItem="{Binding SelectedEntity}">

This way allows you to decouple all UI-controls from each other and modify the View with easy. 通过这种方式,您可以使所有UI控件彼此分离,并轻松修改视图。

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

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