C# 将处理程序添加到FrameworkElementFactory组合框-类型错误?
在阅读了许多关于这个显然令人烦恼的问题的答案之后,包括这篇似乎指明了方向的优秀回复:,在尝试了几天在这个网站上阅读的各种不同建议之后,我仍然无法在我的设置中实现这一点 因此,基础数据结构的公共成员是:C# 将处理程序添加到FrameworkElementFactory组合框-类型错误?,c#,wpf,frameworkelementfactory,C#,Wpf,Frameworkelementfactory,在阅读了许多关于这个显然令人烦恼的问题的答案之后,包括这篇似乎指明了方向的优秀回复:,在尝试了几天在这个网站上阅读的各种不同建议之后,我仍然无法在我的设置中实现这一点 因此,基础数据结构的公共成员是: #region Public Members public EMSBasicDevice Device { get => _Device; set => _Device = value; } public BitmapImage Dev
#region Public Members
public EMSBasicDevice Device { get => _Device; set => _Device = value; }
public BitmapImage DeviceIcon { get => _DeviceIcon; set => _DeviceIcon = value; }
public string DeviceIconPath { get => _DeviceIconPath; set => _DeviceIconPath = value; }
public int RowHeight { get => _RowHeight; set => _RowHeight = value; }
public Color RowColour { get => _RowColour; set => _RowColour = value; }
public string DataDescription { get => _DataDescription; set => _DataDescription = value; }
public string ZoneText { get => _Zone; set => _Zone = value; }
public string LocationText { get => _LocationText; set => _LocationText = value; }
public bool IsHeader { get => _IsHeader; set => _IsHeader = value; }
public InputOutput IOType { get => _IOType; set => _IOType = value; }
public CellControl ShowCombo { get => _ShowCombo; set => _ShowCombo = value; }
public int IOPoint { get => _IOPoint; set => _IOPoint = value; }
public int IOIndex { get => _IOIndex; set => _IOIndex = value; }
public int RowNumber { get => _RowNumber; set => _RowNumber = value; }
public Visibility SensorVis { get => _SensorVis; set => _SensorVis = value; }
public IEnumerable ItemsSource { get => _ItemsSource; set => _ItemsSource = value; }
public object SelectedItem { get => _SelectedItem; set => _SelectedItem = value; }
public SelectionChangedEventHandler OnChange;
#endregion
目标UI元素是DataGrid。所有列都在代码中显式创建。下面是第4列的代码,其中包含一个组合框。为选项列表(ItemsSource)、所选项、是否显示控件(不是在网格的每一行)和(尝试)传递对“更改处理程序”的引用提供绑定。Factory元素组合框没有更改处理程序的DP。因此,我的代码如下所示,遵循上面链接中给出的信息:
// Fourth Column: Sensor Settings
DataGridTemplateColumn SensorSettingCol = new DataGridTemplateColumn();
Binding SensorChoicesBind = new Binding("ItemsSource")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
};
Binding SensorSettingBind = new Binding("SelectedItem")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
NotifyOnSourceUpdated = true
};
Binding SensorVisibility = new Binding("SensorVis")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
Binding OnChangeBinding = new Binding("OnChange")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
FrameworkElementFactory cbInstance1 = new FrameworkElementFactory(typeof(ComboBox));
ImageTemplate = new DataTemplate();
SensorSettingCol.CellTemplate = ImageTemplate;
SensorSettingCol.Width = new DataGridLength(150);
SensorSettingCol.CanUserSort = false;
ImageTemplate.VisualTree = cbInstance1;
cbInstance1.SetBinding(ComboBox.ItemsSourceProperty, SensorChoicesBind);
cbInstance1.SetBinding(ComboBox.SelectedValueProperty, SensorSettingBind);
cbInstance1.SetBinding(ComboBox.VisibilityProperty, SensorVisibility);
cbInstance1.AddHandler(ComboBox.SelectionChangedEvent, new RoutedPropertyChangedEventHandler<object>(SelectionChanged));
TheDataGrid.Columns.Add(SensorSettingCol);
The handler looks like this:
public void SelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//do stuff with e.OriginalSource
}
Of course, this is only half the problem - the real issue is accessing the actual handler in the dataset
which should be called when the user selects a different choice in the ComboBox. Note that the
implementation is intended to be as generic as possible - the combo boxes on different rows access
different choices based upon the underlying dataset.
The error message is:
System.ArgumentException
HResult=0x80070057
Message=Handler type is not valid.
Source=PresentationFramework
StackTrace:
at System.Windows.FrameworkElementFactory.AddHandler(RoutedEvent routedEvent, Delegate handler, Boolean handledEventsToo)
at System.Windows.FrameworkElementFactory.AddHandler(RoutedEvent routedEvent, Delegate handler)
at EMS_Config_Tool.UIComponents.WPF.DeviceEditControl.SetupDeviceGrid(DataGrid TheDataGrid) in C:\Workspace\90504_SmartCellConfigTool\Trunk\EMS Config Tool\UIComponents\WPF\DeviceEditControl.xaml.cs:line 794
The question is: The handler type is wrong, so what should the type actually be?
And for a Gold Star, is there a MUCH BETTER way to link the SelectionChanged event
to the appropriate handler?
//第四列:传感器设置
DataGridTemplateColumn SensorSettingCol=新DataGridTemplateColumn();
Binding SensorChoicesBind=新绑定(“ItemsSource”)
{
Mode=BindingMode.TwoWay,
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged,
};
绑定传感器设置绑定=新绑定(“SelectedItem”)
{
Mode=BindingMode.TwoWay,
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged,
NotifyOnSourceUpdated=true
};
绑定SensorVisibility=新绑定(“SensorVis”)
{
Mode=BindingMode.TwoWay,
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged
};
Binding OnChangeBinding=新绑定(“OnChange”)
{
Mode=BindingMode.TwoWay,
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged
};
FrameworkElementFactory cbInstance1=新FrameworkElementFactory(类型(组合框));
ImageTemplate=新数据模板();
SensorSettingCol.CellTemplate=图像模板;
SensorSettingCol.Width=新数据网格长度(150);
SensorSettingCol.CanUserSort=false;
ImageTemplate.VisualTree=cbInstance1;
cbInstance1.SetBinding(ComboBox.ItemsSourceProperty,SensorChoicesBind);
cbInstance1.SetBinding(ComboBox.SelectedValueProperty,SensorSettingBind);
cbInstance1.SetBinding(ComboBox.VisibilityProperty,传感器可见性);
cbInstance1.AddHandler(ComboBox.SelectionChangedEvent,新RoutedPropertyChangedEventHandler(SelectionChanged));
DataGrid.Columns.Add(传感器设置列);
处理程序如下所示:
public void SelectionChanged(对象发送方,RoutedPropertyChangedEventArgs e)
{
//用e.OriginalSource做一些事情
}
当然,这只是问题的一半——真正的问题是访问数据集中的实际处理程序
当用户在组合框中选择其他选项时,应调用该选项。请注意
实现的目的是尽可能通用-不同行上的组合框访问
基于基础数据集的不同选择。
错误消息是:
System.ArgumentException
HResult=0x80070057
消息=处理程序类型无效。
Source=PresentationFramework
堆栈跟踪:
在System.Windows.FrameworkElementFactory.AddHandler(RoutedEvent RoutedEvent、委托处理程序、布尔HandleEventsToo)
位于System.Windows.FrameworkElementFactory.AddHandler(RoutedEvent RoutedEvent,委托处理程序)
在C:\Workspace\90504\u SmartCellConfigTool\Trunk\EMS Config Tool\UIComponents\WPF\DeviceEditControl.SetupDeviceGrid(数据网格)中的EMS\u Config\u Tool.UIComponents.WPF.DeviceEditControl.SetupDeviceGrid(数据网格):第794行
问题是:处理程序类型是错误的,那么该类型实际上应该是什么?
对于一个金星来说,有没有更好的方式来链接SelectionChanged事件
给合适的处理者?