C# 使用XamDataGrid并尝试从RecordActivated()函数中获取发送方对象的特殊值
你好 我被一个问题困住了,我想不出解决的办法。C# 使用XamDataGrid并尝试从RecordActivated()函数中获取发送方对象的特殊值,c#,wpf,xaml,infragistics,C#,Wpf,Xaml,Infragistics,你好 我被一个问题困住了,我想不出解决的办法。 由于我对基础设施和wpf没有太多经验,我首先创建了一个测试项目来尝试如何正确地完成它 我有一个包含组合框的xamComboEditor。 当我打开组合框时,我得到了一个包含“states”表的xamDataGrid。 选择一行后,我需要将所选行的主键写入组合框。 我这里的问题是,当RecordActivated()事件触发时,我得到了sender对象,但我不知道如何从中读取所需的数据。 当我使用VisualStudio观察列表工具查看sender对
由于我对基础设施和wpf没有太多经验,我首先创建了一个测试项目来尝试如何正确地完成它 我有一个包含组合框的xamComboEditor。
当我打开组合框时,我得到了一个包含“states”表的xamDataGrid。 选择一行后,我需要将所选行的主键写入组合框。
我这里的问题是,当RecordActivated()事件触发时,我得到了sender对象,但我不知道如何从中读取所需的数据。
当我使用VisualStudio观察列表工具查看sender对象时,我可以找到我需要的数据,但我不知道如何获得它的价值 我需要获取sender对象内部的“row”值,它已经非常隐藏,无法通过watchlist工具找到。 搜索了一段时间后,我找到了正确的值。 我在那里找到的:
发件人->活动记录->数据项->行 Test.xaml.cs:
namespace WpfApplication1
{
public partial class test : Window
{
public test()
{
InitializeComponent();
runFASTWMVDataSetTableAdapters.StaatenTableAdapter staat = new runFASTWMVDataSetTableAdapters.StaatenTableAdapter();
combo.ItemsSource = staat.GetData().DefaultView;
}
//MainGrid2 is the grid which is in my combobox
private void MainGrid2_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
{
//combo is my comboBox object
combo.IsDropDownOpen = false;
//guess i need to somehow read the "row" from the sender object right here.
}
}
}
我将发布xaml的各个部分,这非常重要,因为我有一个相当长的xaml
test.xaml:
<igWPF:XamComboEditor x:Name="combo" Margin="1,1,1,219" ComboBoxStyle="{DynamicResource ComboStyle}"
DisplayValueSource="{Binding SelectedItem, RelativeSource={RelativeSource Self}}">
<Grid x:Name="MainGrid" SnapsToDevicePixels="True">
<igWPF:XamDataGrid x:Name="MainGrid2" DataSource="{TemplateBinding ItemsSource}"
RecordActivated="MainGrid2_RecordActivated" GroupByAreaLocation="None" SelectedDataItemsScope="RecordsOnly"/>
</Grid>
</igWPF:XamComboEditor>
在苦苦挣扎和谷歌搜索了几个小时后,我已经没有任何想法可以尝试了,所以我想我可以请你们帮忙。
希望我在为帖子格式化代码时没有删除任何内容,因为它确实可以像我本地项目中预期的那样工作。如果我的帖子中有语法错误,也很抱歉,英语不是我的第一语言 你问这个问题已经有一段时间了,但我想回答你的问题,即使你不再使用网格。也许这个答案会帮助其他人
我创建了一个示例,它将向您展示:
我的示例包含以下类:
Person.cs
namespace XamDataGridDemo
{
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public override string ToString()
{
return $"{LastName}, {FirstName} ({Age})";
}
}
}
main window.xaml
<Window x:Class="XamDataGridDemo.MainWindow"
x:Name="Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ig="http://schemas.infragistics.com/xaml"
xmlns:dp="http://infragistics.com/DataPresenter"
xmlns:editors="http://infragistics.com/Editors"
Title="XamDataGridDemo" Height="350" Width="525"
DataContext="{Binding ElementName=Demo}"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Reference InfragisticsWPF4.v16.1.dll -->
<!-- Reference InfragisticsWPF4.DataPresenter.v16.1.dll -->
<dp:XamDataGrid x:Name="FirstPersonGrid"
Grid.Row="0"
DataSource="{Binding Path=Persons}"
RecordActivated="PersonGrid_OnRecordActivated"
/>
<Separator Grid.Row="1" Margin="3" />
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Reference InfragisticsWPF4.Editors.v16.1.dll -->
<editors:XamComboEditor x:Name="Combo"
Grid.Row="0"
ItemsSource="{Binding Path=Persons}"
SelectedItem="{Binding ElementName=SecondPersonGrid, Path=ActiveDataItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
<!-- Reference InfragisticsWPF4.DataPresenter.v16.1.dll -->
<dp:XamDataGrid x:Name="SecondPersonGrid"
Grid.Row="1"
DataSource="{Binding Path=Persons}"
/>
</Grid>
<Separator Grid.Row="3" Margin="3" />
<!-- Reference InfragisticsWPF4.Controls.Editors.XamComboEditor.v16.1.dll -->
<ig:XamMultiColumnComboEditor Grid.Row="4"
ItemsSource="{Binding Path=Persons}"
EmptyText="Select a Person ..."
SelectedValue="{Binding Path=SelectedItem.Id, RelativeSource={RelativeSource Self}}"
SelectionChanged="XamMultiColumnComboEditor_OnSelectionChanged"
/>
</Grid>
</Window>
namespace XamDataGridDemo
{
using System.Collections.Generic;
using System.Windows;
using Infragistics.Controls.Editors;
using Infragistics.Windows.DataPresenter;
using Infragistics.Windows.DataPresenter.Events;
public partial class MainWindow : Window
{
public MainWindow()
{
this.Persons = new List<Person>
{
new Person { Id = 0, FirstName = "Horst", LastName = "Horstenson", Age = 55 },
new Person { Id = 1, FirstName = "Hilde", LastName = "Wild", Age = 45 },
new Person { Id = 2, FirstName = "Klaus", LastName = "Klausen", Age = 50 }
};
DataContext = this.Persons;
InitializeComponent();
}
public IList<Person> Persons { get; }
private void PersonGrid_OnRecordActivated(object sender, RecordActivatedEventArgs e)
{
var xamDataGrid = (XamDataGrid)sender;
var activePerson = xamDataGrid.ActiveDataItem as Person;
if (activePerson != null)
{
// if you want to do something with the properties
int id = activePerson.Id;
string firstName = activePerson.FirstName;
string lastName = activePerson.LastName;
int age = activePerson.Age;
MessageBox.Show($"You have selected {activePerson}", "Selected Person");
}
// Second Approch -- Your Watchlist
var record = xamDataGrid.ActiveRecord as DataRecord;
activePerson = record?.DataItem as Person;
if (activePerson != null)
{
MessageBox.Show($"You have selected {activePerson}", "Selected Person");
}
}
private void XamMultiColumnComboEditor_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var xamMultiColumnComboEditor = (XamMultiColumnComboEditor)sender;
var activePerson = xamMultiColumnComboEditor.SelectedItem as Person;
if (activePerson == null)
{
return;
}
MessageBox.Show($"You have selected {activePerson}", "Selected Person");
// Have to add reference to InfragisticsWPF4.DataManager.v16.1.dll
if (xamMultiColumnComboEditor.SelectedValue is int)
{
int id = (int)xamMultiColumnComboEditor.SelectedValue;
MessageBox.Show($"You have selected the person with id: {id}", "Selected Value");
}
}
}
}
1。如何从发送方对象中获取特殊值? 正如您在
void PersonGrid\u on recordactivated(object sender,RecordActivatedEventArgs e)
中所看到的,我假设发件人是XamDataGrid
,我检查ActiveDataItem
是否是我类型的Person
对象或者,如果您想遵循您的观察列表方法。您可以使用casted
sender
并检查ActiveRecord
是否是属于我的Person类型的DataRecord
SelectedValue="{Binding Path=SelectedItem.Id, RelativeSource={RelativeSource Self}}"
现在你可以用你的“行”对象做任何你想做的事情
2。如何将XamDataGrid的ActiveRecord绑定到XamComboEditor的SelectedItem?
我有点懒,所以我将第二个示例的内容缩短了一点,并在XamComboEditor
之外使用XamDataGrid
,但它也应该在嵌套网格中工作,因为我使用了带有ElementName的绑定
如您所见,我刚刚将XamComboEditor
的SelectenItem绑定到XamDataGrid
的ActiveDataItem
SelectedItem="{Binding ElementName=SecondPersonGrid, Path=ActiveDataItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
我还使用Mode=TwoWay
和UpdateSourceTrigger=PropertyChanged
将ActiveDataItem
设置为SelectedItem
3。另一种在嵌套数据网格中使用组合框的方法。
正如您已经尝试过的,您应该看看xammultipolumncomboeditor
,正如其名称所示,它是一个包含可以定义的列的组合框。
您可以使用与1中相同的方法。要从发件人处获取所选的人员
。请参见上述代码中的void xammultipolumncomboeditor\u OnSelectionChanged(对象发送者,selectionchangedventargs e)
。
为了满足您的需要,为了获取“行id”,我将SelectedValue绑定到所选人员的id
属性
SelectedValue="{Binding Path=SelectedItem.Id, RelativeSource={RelativeSource Self}}"
我使用了Infragistics v16.1,不得不在我的项目中添加以下参考
- InfragisticsWPF4.Controls.Editors.XamComboEditor.v16.1.dll
- InfragisticsWPF4.DataManager.v16.1.dll
- InfragisticsWPF4.DataPresenter.v16.1.dll
- InfragisticsWPF4.Editors.v16.1.dll
- InfragisticsWPF4.Editors.v16.1.dll
如果有什么不清楚或太宽泛的地方,请随时要求澄清。经过一段时间的努力,我们的老板决定开始使用DevExpress而不是Infrastics,因为它在我们遇到的大多数情况下都能更好地工作,并为我们的问题找到了解决方案。完全忘记这个问题。在你回答这个问题的时候,我已经开始了新的项目,不过还是要感谢你提供的解决方案。