我在2012年10月问到this问题,当时我正在开发Windows应用程序。现在,当我转向WPF应用程序时,我再次遇到同样的问题,即如何在 WPF 中获取DevExpress GridControl的选定行值?我在谷歌找不到答案,上面提到的链接中没有一个答案正常。在wpf的devexpress gridcontrol中没有像CellClick,RowClick或RowCellClick事件那样,因为它在winform gridcontrol中。如果有人能解决这个问题,我会很高兴
修改
我已经在您的答案中更新了我的应用程序所需的命名空间,但问题仍然存在。我在
中遇到了拖拽错误<Window x:Class="Namespace.MainWindow"
...
x:Name="ololo"
Height="400" Width="500">
.....
<StackPanel Orientation="Horizontal">
<StackPanel x:Name="Left" Orientation="Vertical">
<Button Margin="0,10,0,0" Command="{Binding HideCommand}">Hide</Button>
</StackPanel>
<StackPanel x:Name="Right" Orientation="Vertical">
<!--<Controls>-->
</StackPanel>
</StackPanel>
1-类型&#39; dxmvvm:ViewModelSource&#39;&#34;在dxmvvm中找不到:ViewModelSource
2- name&#34; EntitiesViewModel&#34;命名空间中不存在&#34; clr-namespace:DXApplication1&#34;
我的申请编码如下
XAML
<Grid.DataContext>
<dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>
</Grid.DataContext>
EntitiesViewModel
<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>
库,错误,类等的图像
答案 0 :(得分:2)
您可以使用DataControlBase.CurrentItemChanged
活动
这是一个例子:
的 WPF:强>
<dxg:GridControl x:Name="gridControl1" ItemsSource="{Binding Data}" CurrentItemChanged="gridControl1_CurrentItemChanged">
</dxg:GridControl>
事件处理程序:
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();
}
答案 1 :(得分:0)
查看Devexpress主页文档中的选择主题: Grid Selection Topic
如果您正在使用MVVM模式,您还应该看一下 SelectedItems Property。 这里的示例显示了如何绑定到选定的项目:
In [113]:
# load your data, you can ignore this step
t="""time,ActivePowerkW,WindSpeedms,WindSpeedmsstd
2015-05-26 11:40:00,836.6328,8.234862,1.414558
2015-05-26 11:50:00,968.5992,8.761620,1.572579
2015-05-26 12:30:00,614.0503,7.267871,1.575504
2015-05-26 13:50:00,945.5604,8.709115,1.527079
2015-05-26 14:00:00,926.6531,8.538967,1.589221
2015-05-26 14:30:00,666.7984,7.590645,1.324495
2015-05-26 14:40:00,911.0134,8.466603,1.708189
2015-05-26 15:10:00,1256.1740,9.868224,1.636775
2015-05-26 15:30:00,1706.7070,11.225540,1.576277"""
df = pd.read_csv(io.StringIO(t), parse_dates=[0], index_col=[0])
df
Out[113]:
ActivePowerkW WindSpeedms WindSpeedmsstd
time
2015-05-26 11:40:00 836.6328 8.234862 1.414558
2015-05-26 11:50:00 968.5992 8.761620 1.572579
2015-05-26 12:30:00 614.0503 7.267871 1.575504
2015-05-26 13:50:00 945.5604 8.709115 1.527079
2015-05-26 14:00:00 926.6531 8.538967 1.589221
2015-05-26 14:30:00 666.7984 7.590645 1.324495
2015-05-26 14:40:00 911.0134 8.466603 1.708189
2015-05-26 15:10:00 1256.1740 9.868224 1.636775
2015-05-26 15:30:00 1706.7070 11.225540 1.576277
In [115]:
# create your timeseries
timeseries_comp = pd.date_range(df.index[0], df.index[len(df)-1], freq='10min')
timeseries_comp
Out[115]:
DatetimeIndex(['2015-05-26 11:40:00', '2015-05-26 11:50:00',
'2015-05-26 12:00:00', '2015-05-26 12:10:00',
'2015-05-26 12:20:00', '2015-05-26 12:30:00',
'2015-05-26 12:40:00', '2015-05-26 12:50:00',
'2015-05-26 13:00:00', '2015-05-26 13:10:00',
'2015-05-26 13:20:00', '2015-05-26 13:30:00',
'2015-05-26 13:40:00', '2015-05-26 13:50:00',
'2015-05-26 14:00:00', '2015-05-26 14:10:00',
'2015-05-26 14:20:00', '2015-05-26 14:30:00',
'2015-05-26 14:40:00', '2015-05-26 14:50:00',
'2015-05-26 15:00:00', '2015-05-26 15:10:00',
'2015-05-26 15:20:00', '2015-05-26 15:30:00'],
dtype='datetime64[ns]', freq='10T', tz=None)
In [120]:
# reindex
new_df = df.reindex(timeseries_comp)
# group on hour and minute, you can group on some other resolution
new_df.groupby([new_df.index.hour, new_df.index.minute]).apply(pd.Series.isnull).sum()
Out[120]:
ActivePowerkW 15
WindSpeedms 15
WindSpeedmsstd 15
dtype: int64
答案 2 :(得分:0)
我发现解决方案如下
<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>
答案 3 :(得分:0)
据我所知,您想要显示实体列表,然后提供用于选择其中一个权限的UI(通过gridcontrol),并在单独的视图中编辑所选实体属性(通过文本编辑器)。
所以,我建议你使用MVVM方法。而且,由于您已经在使用DevExpress控件,我建议您使用DevExpress MVVM Framework尽可能简化MVVM。
步骤1:定义一个ViewModel类,其中包含通过Entities
属性提供的实体集合(您可以根据需要以所需的方式加载这些实体,并在需要时)并提供{{1 property:
SelectedEntity
步骤2:按如下方式定义视图布局:
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; }
}
*主要兴趣点和技巧是:
1)通过ViewModelSource创建...
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>
实例。这种方式允许您不在ViewModel级别实现EntitiesViewModel
接口 - 您可以将INotifyPropertyChanged
属性声明为SelectedEntity
并将所有脏工作委托给POCO mechanism :
virtual
2)所有View'控件绑定都“查找”到DataContext(包含我们的ViewModel):
<dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>
这种方式允许您将所有UI控件相互分离,并轻松修改视图。