如何以编程方式更新自动生成的datagrid组合框单元格中的项目源 [英] How to update programmatically the items source in an autogenerated datagrid combobox cell
问题描述
我找不到我的问题的解决方案
我有一个绑定到对象集合的datagrid。我的对象属性之一用作集合中的索引。自动生成的组合框列类型显示与此索引相关联的标签。
我需要更新另一个属性,该属性也是同一对象中的索引。我使用此代码在AutogeneratingColumn事件中添加组合框:
public partial class LedTableEditor:MetroWindow,INotifyPropertyChanged
{
私有对象_sender;
public Dictionary< int,string> IdColors = new Dictionary< int,string>();
public ObservableCollection< Xceed.Wpf.Toolkit.ColorItem> MarshallingColors = new ObservableCollection< Xceed.Wpf.Toolkit.ColorItem>();
私人字典< int,string> cbTypeVals = new Dictionary< int,string>();
私人字典< UInt16,string> cbSrcValueVals = new Dictionary< UInt16,string>();
私人字典< UInt16,string> cbDiagVals = new Dictionary< UInt16,string>();
私人列表< string> ListeData = new List< string>();
私人列表< string> ListeDiag = new List< string>();
private int maxLeds = 16;
私人XapLedVals带领;
public LedTableEditor(object senderParam)
{
-----
}
私人void DgLedTable_AutoGeneratingColumn(object sender,DataGridAutoGeneratingColumnEventArgs e)
{
if(e.PropertyName ==Type)
{
DataGridComboBoxColumn cbType = new DataGridComboBoxColumn();
cbType.EditingElementStyle = new Style(typeof(ComboBox))
{
Setters =
{
new EventSetter(Selector.SelectionChangedEvent,new SelectionChangedEventHandler(OnComboBoxSelectionChanged))
}
};
e.Column = cbType;
cbType.ItemsSource = cbTypeVals; // new List< string> {eLedType.ALERTE.ToString(),eLedType.DIAG.ToString(),eLedType.TRIGGER.ToString()};
cbType.DisplayMemberPath =Value;
cbType.SelectedValuePath =Key;
cbType.SelectedValueBinding = new Binding(Type);
e.Column.Header =Type;
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(新的Setter(DataGridCell.HorizontalAlignmentProperty,HorizontalAlignment.Stretch));
}
if(e.PropertyName ==Binding)
{
DataGridComboBoxColumn cbBinding = new DataGridComboBoxColumn();
BindingOperations.SetBinding(cbBinding,DataGridComboBoxColumn.ItemsSourceProperty,new Binding(Source){Source = this});
e.Column = cbBinding;
cbBinding.ItemsSource = cbSrcValueVals;
cbBinding.DisplayMemberPath =Value;
cbBinding.SelectedValuePath =Key;
cbBinding.SelectedValueBinding = new Binding(Binding);
e.Column.Header =Binding;
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(新的Setter(DataGridCell.HorizontalAlignmentProperty,HorizontalAlignment.Stretch));
}
----
}
组合框类型项目来源是字典:
私人字典< int,string> cbTypeVals = new Dictionary< int,string>();
我需要更新此绑定组合框项目源,它在同一个datagrid中也是自动生成的:
if(e.PropertyName ==Binding)
{
AutoCommitComboBoxColumn cb = new AutoCommitComboBoxColumn );
e.Column = cb;
cb.ItemsSource = cbSrcValueVals;
cb.DisplayMemberPath =Value;
cb.SelectedValuePath =Key;
cb.SelectedValueBinding = new Binding(Binding);
e.Column.Header =Binding;
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(新的Setter(DataGridCell.HorizontalAlignmentProperty,HorizontalAlignment.Stretch));
}
comboboxBinding项目源是一个字典:
私人字典< UInt16,string> cbSrcValueVals = new Dictionary< UInt16,string>();
应该使用这个更新:
私人字典< UInt16,string> cbDiagVals = new Dictionary< UInt16,string>();
事件正确触发,但我找不到更新绑定组合框项目的方法
private void OnComboBoxSelectionChanged(object sender,SelectionChangedEventArgs e)
{
DgLedTable.CurrentCell = new DataGridCellInfo (DgLedTable.SelectedIndex,DgLedTable.Columns [1]);
}
感谢您的帮助。
此处编辑的是具有ComboBoxType项目的屏幕图像:
编辑,添加的财产已更改:
private System.Collections。 IEnumerable _source;
public System.Collections.IEnumerable Source
{
get {return _source; }
set {_source = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
private void OnComboBoxSelectionChanged(object sender,SelectionChangedEventArgs e)
{
// DgLedTable.CurrentCell = new DataGridCellInfo(DgLedTable.SelectedIndex,DgLedTable.Columns [1 ]);
源= cbDiagVals;
}
如果您创建源属性并将Binding ComboBox
的 ItemsSource
属性绑定到该属性中,您可以将此属性设置为新集合Type ComboBox的
SelectionChanged
事件处理程序。
窗口 - 或者您的代码定义的任何类型 - 必须实现 INotifyPropertyChanged 这个工作接口:
public partial class MainWindow:Window, INotifyPropertyChanged
{
私人字典< int,string> cbTypeVals = new Dictionary< int,string>();
私人字典< UInt16,string> cbSrcValueVals = new Dictionary< UInt16,string>();
私有字典< UInt16,string> cbDiagVals = new Dictionary< UInt16,string>();
public MainWindow()
{
InitializeComponent();
_source = cbTypeVals;
// ...
}
private System.Collections.IEnumerable _source;
public System.Collections.IEnumerable Source
{
get {return _source; }
set {_source = value; OnPropertyChanged();
}
private void OnAutoGeneratingColumn(object sender,DataGridAutoGeneratingColumnEventArgs e)
{
if(e.PropertyName ==Binding)
{
AutoCommitComboBoxColumn cb = new AutoCommitComboBoxColumn();
e.Column = cb;
//将ItemsSource属性绑定到窗口的Source属性...
cb.SetBinding(AutoCommitComboBoxColumn.ItemsSourceProperty,new Binding(Source){Source = this}) ;
cb.ItemsSource = cbSrcValueVals;
cb.DisplayMemberPath =Value;
cb.SelectedValuePath =Key;
cb.SelectedValueBinding = new Binding(Binding);
e.Column.Header =Binding;
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(新的Setter(DataGridCell.HorizontalAlignmentProperty,HorizontalAlignment.Stretch));
}
// ...
}
private void OnComboBoxSelectionChanged(object sender,SelectionChangedEventArgs e)
{
//设置将Source属性的值添加到新集合,并在此处引发PropertyChanged事件...
Source = cbDiagVals;
}
public event PropertyChangedEventHandler PropertyChanged;
private virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
// ...
}
I can't find a working solution to my issue
I have a datagrid binded to an object collection. one of my object property is used as an index in a collection. The autogenerated combobox column "Type" displays the "label" associated to this index.
I need to update another property which is an index too in that same object. I use this code to add the combobox in the AutogeneratingColumn event :
public partial class LedTableEditor : MetroWindow, INotifyPropertyChanged
{
private object _sender;
public Dictionary<int, string> IdColors = new Dictionary<int, string>();
public ObservableCollection<Xceed.Wpf.Toolkit.ColorItem> MarshallingColors = new ObservableCollection<Xceed.Wpf.Toolkit.ColorItem>();
private Dictionary<int, string> cbTypeVals = new Dictionary<int, string>();
private Dictionary<UInt16, string> cbSrcValueVals = new Dictionary<UInt16, string>();
private Dictionary<UInt16, string> cbDiagVals = new Dictionary<UInt16, string>();
private List<string> ListeData = new List<string>();
private List<string> ListeDiag = new List<string>();
private int maxLeds = 16;
private XapLedVals led;
public LedTableEditor(object senderParam)
{
-----
}
private void DgLedTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Type")
{
DataGridComboBoxColumn cbType = new DataGridComboBoxColumn();
cbType.EditingElementStyle = new Style(typeof(ComboBox))
{
Setters =
{
new EventSetter(Selector.SelectionChangedEvent, new SelectionChangedEventHandler(OnComboBoxSelectionChanged))
}
};
e.Column = cbType;
cbType.ItemsSource = cbTypeVals; // new List<string> { eLedType.ALERTE.ToString(), eLedType.DIAG.ToString(), eLedType.TRIGGER.ToString() };
cbType.DisplayMemberPath = "Value";
cbType.SelectedValuePath = "Key";
cbType.SelectedValueBinding = new Binding("Type");
e.Column.Header = "Type";
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch));
}
if (e.PropertyName == "Binding")
{
DataGridComboBoxColumn cbBinding = new DataGridComboBoxColumn();
BindingOperations.SetBinding(cbBinding, DataGridComboBoxColumn.ItemsSourceProperty, new Binding("Source") { Source = this });
e.Column = cbBinding;
cbBinding.ItemsSource = cbSrcValueVals;
cbBinding.DisplayMemberPath = "Value";
cbBinding.SelectedValuePath = "Key";
cbBinding.SelectedValueBinding = new Binding("Binding");
e.Column.Header = "Binding";
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch));
}
----
}
the combobox "Type" item source is a dictionary :
private Dictionary<int, string> cbTypeVals = new Dictionary<int, string>();
I need to update this "Binding" combobox item source, which is autogenerated too in the same datagrid :
if (e.PropertyName == "Binding")
{
AutoCommitComboBoxColumn cb = new AutoCommitComboBoxColumn();
e.Column = cb;
cb.ItemsSource = cbSrcValueVals;
cb.DisplayMemberPath = "Value";
cb.SelectedValuePath = "Key";
cb.SelectedValueBinding = new Binding("Binding");
e.Column.Header = "Binding";
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch));
}
the combobox "Binding" item source is a dictionary :
private Dictionary<UInt16, string> cbSrcValueVals = new Dictionary<UInt16, string>();
it should be updated with this one :
private Dictionary<UInt16, string> cbDiagVals = new Dictionary<UInt16, string>();
The event is correctly fired, but I can't find a way to update "Binding" combobox items.
private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
DgLedTable.CurrentCell = new DataGridCellInfo(DgLedTable.SelectedIndex, DgLedTable.Columns[1]);
}
Thank you for your help.
Edit here is an image of the screen with items in ComboBox "Type" :
Edit, code added for property changed :
private System.Collections.IEnumerable _source;
public System.Collections.IEnumerable Source
{
get { return _source; }
set { _source = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// DgLedTable.CurrentCell = new DataGridCellInfo(DgLedTable.SelectedIndex, DgLedTable.Columns[1]);
Source = cbDiagVals;
}
If you create a source property and bind the ItemsSource
property of the "Binding" ComboBox
to this one you could set this property to a new collection in the "Type" ComboBox's
SelectionChanged
event handler.
The window - or in whatever type your code is defined - must implement the INotifyPropertyChanged interface for this to work:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private Dictionary<int, string> cbTypeVals = new Dictionary<int, string>();
private Dictionary<UInt16, string> cbSrcValueVals = new Dictionary<UInt16, string>();
private Dictionary<UInt16, string> cbDiagVals = new Dictionary<UInt16, string>();
public MainWindow()
{
InitializeComponent();
_source = cbTypeVals;
//...
}
private System.Collections.IEnumerable _source;
public System.Collections.IEnumerable Source
{
get { return _source; }
set { _source = value; OnPropertyChanged(); }
}
private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Binding")
{
AutoCommitComboBoxColumn cb = new AutoCommitComboBoxColumn();
e.Column = cb;
//bind the ItemsSource property to the Source property of the window here...
cb.SetBinding(AutoCommitComboBoxColumn.ItemsSourceProperty, new Binding("Source") { Source = this });
cb.ItemsSource = cbSrcValueVals;
cb.DisplayMemberPath = "Value";
cb.SelectedValuePath = "Key";
cb.SelectedValueBinding = new Binding("Binding");
e.Column.Header = "Binding";
e.Column.CellStyle = new Style(typeof(DataGridCell));
e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch));
}
//...
}
private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
//set the value of the Source property to a new collection and raise the PropertyChanged event here...
Source = cbDiagVals;
}
public event PropertyChangedEventHandler PropertyChanged;
private virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
//...
}
这篇关于如何以编程方式更新自动生成的datagrid组合框单元格中的项目源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!