你好,
我遇到了问题,我无法找到解决办法 由于我没有很多infragistics和wpf的经验,我首先创建了一个测试项目来尝试如何正确地完成它。
我有一个包含comboBox的xamComboEditor
当我打开comboBox时,我得到了一个带有"状态" -table的xamDataGrid。
选择一行后,我需要将所选行的主键写入comboBox
我的问题是我得到了发件人对象,当RecordActivated() - 事件触发但我不知道如何从中读取所需的数据。
当我使用VisualStudio监视列表工具查看发送者对象时,我可以找到我需要的数据,但我不知道如何获得它的价值。
我需要得到"行" sender-object内部的值已经非常隐藏,可以通过watchlist-tool查找。
搜索了一会后,我发现了正确的价值观。
我在那里找到了:
发件人 - > ActiveRecord - > dataItem - >行
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>
经过几个小时的挣扎和谷歌搜索后,我没有任何想法可以尝试,所以我想我可能会请你们帮忙。
希望在格式化帖子的代码时我没有删除任何内容,它确实像我本地项目那样工作。答案 0 :(得分:0)
你提出问题已经有一段时间了,但我想回答你的问题,即使你不再使用网格了。也许这个答案会有所帮助。
我创建了一个示例,它将向您显示:
我的示例包含以下类:
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})";
}
}
}
<强> MainWindow.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>
<强> MainWindow.xaml.cs
强>
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_OnRecordActivated(object sender, RecordActivatedEventArgs e)
中看到的那样,我假设发件人是XamDataGrid
,我会检查ActiveDataItem
是否是我的类型Person
的对象。
或者,如果您想要遵循您的监视列表方法。您可以使用已投放的sender
并检查ActiveRecord
是否属于DataRecord
类Person
。
现在,您可以使用“行”对象执行任何操作。
<强> 2。如何将XamDataGrid的ActiveRecord绑定到XamComboEditor的SelectedItem?
我有点懒,所以我稍微缩短了第二个例子并在XamDataGrid
之外使用XamComboEditor
,但它也应该在嵌套网格中工作,因为我使用了使用ElementName绑定。
因此,您可以看到我只是将XamComboEditor
的SelectenItem绑定到ActiveDataItem
的{{1}}。
XamDataGrid
我还使用SelectedItem="{Binding ElementName=SecondPersonGrid, Path=ActiveDataItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
和Mode=TwoWay
将UpdateSourceTrigger=PropertyChanged
设置为ActiveDataItem
。
第3。嵌套类型的DataGrid中的另一种ComboBox方法。
正如您已经尝试过的infragistics,您应该看看SelectedItem
,正如名称所示,这是一个可以定义列的组合框。
您可以使用与1.中相同的方法从发件人处获取所选的XamMultiColumnComboEditor
。请参阅上面代码中的Person
。
为了满足您的需求,为了获得“row-id”,我将SelectedValue绑定到所选void XamMultiColumnComboEditor_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
的{{1}}属性。
Id
我使用 Infragistics v16.1 ,并且必须将以下引用添加到我的项目中。
如果某些内容不清楚或广泛,请随时要求澄清。