- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
你好,
我被一个问题困住了,我想不出解决办法。
由于我在基础设施和 wpf 方面没有太多经验,所以我首先创建了一个测试项目来尝试如何正确地完成它。
我有一个包含组合框的 xamComboEditor。
当我打开组合框时,我得到了一个带有“状态”表的 xamDataGrid。选择一行后,我需要将所选行的主键写入组合框。
我的问题是,当 RecordActivated()-Event 触发时,我得到了发送者对象,但我不知道如何从中读取我需要的数据。
当我使用 VisualStudio 监视列表工具查看发件人对象时,我可以找到我需要的数据,但我不知道如何获取它的值。
我需要获取发件人对象内的“行”值,该值已经非常隐藏,无法使用监视列表工具找到。搜索了一段时间后,我找到了正确的值。我在那里找到了它:
发件人 -> ActiveRecord -> 数据项 -> 行
测试.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。
测试.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})";
}
}
}
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
是否是我的 Person
类型的 DataRecord
。
现在您可以使用“行”对象做任何您想做的事情。
<强>2。如何将 XamDataGrid 的 ActiveRecord 绑定(bind)到 XamComboEditor 的 SelectedItem?
我有点懒,所以我稍微缩短了第二个示例的内容,并在 XamComboEditor
之外使用了 XamDataGrid
,但它也应该在嵌套的网格,因为我将绑定(bind)与 ElementName 结合使用。
如您所见,我只是将 XamComboEditor
的 SelectenItem 绑定(bind)到 XamDataGrid
的 ActiveDataItem
。
SelectedItem="{Binding ElementName=SecondPersonGrid, Path=ActiveDataItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
我还使用 Mode=TwoWay
和 UpdateSourceTrigger=PropertyChanged
将 ActiveDataItem
设置为 SelectedItem
。
<强>3。 ComboBox 在 DataGrid 的嵌套类型中的另一种方法。
正如您已经尝试过的那样 infragistics您应该看一下 XamMultiColumnComboEditor
,顾名思义,这是一个包含您可以定义的列的组合框。您可以使用与 1. 中相同的方法从发件人处获取选定的 Person
。请参阅上面代码中的 void XamMultiColumnComboEditor_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
。为了满足您的需要,为了获得“row-id”,我将 SelectedValue 绑定(bind)到所选 Person
的 Id
属性。
SelectedValue="{Binding Path=SelectedItem.Id, RelativeSource={RelativeSource Self}}"
我使用了 Infragistics v16.1 并且必须将以下引用添加到我的项目中。
如果有什么不清楚或范围很广,请随时要求澄清。
关于c# - 使用 XamDataGrid 并尝试从 RecordActivated() 函数的发送者对象中获取特殊值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40990788/
你好, 我被一个问题困住了,我想不出解决办法。 由于我在基础设施和 wpf 方面没有太多经验,所以我首先创建了一个测试项目来尝试如何正确地完成它。 我有一个包含组合框的 xamComboEditor。
我是一名优秀的程序员,十分优秀!