如何在 WPF 中获取 DevExpress GridControl 的选定行值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32393926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the selected row values of DevExpress GridControl in WPF?
提问by kashif
I asked thisquestion in Oct, 2012 when I was working on windows application. now when I turn to WPF application, I get the same problem again i.e How to get the selected row values of DevExpress GridControl in WPF? I've failed to find my answer on google and none of the answers in the above mentioned link is working. there is nothing like CellClick, RowClick or RowCellClick event in devexpress gridcontrol of wpf as it is in winform gridcontrol. I'll be glad if someone can solve out this problem
我在 2012 年 10 月在开发 Windows 应用程序时问了这个问题。现在,当我转向 WPF 应用程序时,我又遇到了同样的问题,即如何在WPF 中获取 DevExpress GridControl 的选定行值?我在谷歌上找不到我的答案,上面提到的链接中的答案都没有奏效。wpf 的 devexpress gridcontrol 中没有 CellClick、RowClick 或 RowCellClick 事件,因为它在 winform gridcontrol 中。如果有人能解决这个问题,我会很高兴
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
实体视图模型
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
库、错误、类等的图像
回答by nempoBu4
You can use DataControlBase.CurrentItemChangedevent.
Here is example:
WPF:
您可以使用DataControlBase.CurrentItemChanged事件。
这是示例:
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();
}
回答by Mastr
Look at the selection topic in the documentation at the Devexpress Homepage: Grid Selection Topic
在 Devexpress 主页查看文档中的选择主题: Grid Selection Topic
If you are using the MVVM pattern you should also have a look at the SelectedItems Property. The example there shows how to bind to the selected items:
如果您使用的是 MVVM 模式,您还应该查看 SelectedItems 属性。那里的示例显示了如何绑定到所选项目:
<dxg:GridControl ItemsSource="{Binding Source}" SelectedItems="{Binding Selection}">
回答by kashif
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>
回答by DmitryG
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. And, since you are already using DevExpress controls, I suggest you use the DevExpress MVVM Frameworkto make your MVVM as easy as it possible.
因此,我建议您使用 MVVM 方法。而且,由于您已经在使用 DevExpress 控件,我建议您使用DevExpress MVVM 框架来使您的 MVVM 尽可能简单。
Step 1: Define a ViewModel class that contains your entities collection available via the Entitiesproperty (you can load these entities in way as it needed, from it needed and when it needed) and provide the SelectedEntityproperty:
第 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:
第二步:定义一个View布局如下:
...
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 EntitiesViewModelinstance via the ViewModelSource. This way allows you do not implement the INotifyPropertyChangedinterface at the ViewModel's level - you can just declare the SelectedEntityproperty as virtualand delegate all the dirty-work to the POCO mechanism:
*主要的兴趣点和技巧是:
1)EntitiesViewModel通过 ViewModelSource创建实例。这种方式允许您不在INotifyPropertyChangedViewModel 级别实现接口 - 您只需将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 控件彼此分离并轻松修改视图。


