如何以编程方式更新自动生成的数据网格组合单元格中的项目源

时间:2017-02-10 13:12:55

标签: c# wpf data-binding autogeneratecolumn

我无法找到解决问题的方法

我有一个绑定到对象集合的数据网格。我的一个object属性用作集合中的索引。自动生成的组合框列“类型”显示与此索引关联的“标签”。

我需要在同一个对象中更新另一个属性索引。我使用此代码在AutogeneratingColumn事件中添加组合框:

package testecase;

public class TesteCase {
    public static void main(String args []) {
        // TODO code application logic here
        switch (args [0] .charAt (0)) {
        case 'A':System.out.println("Vogal A ");
        break;

        case 'E':System.out.println("Vogal E ");
        break;

        default:System.out.println("Não é vogal ");

        }
    }
}

组合框“类型”项目来源是字典:

  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));
        }
----
   }

我需要更新这个“绑定”组合框项目源,它也是在同一个数据网格中自动生成的:

private Dictionary<int, string> cbTypeVals = new Dictionary<int, string>();

组合框“Binding”项目来源是字典:

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));
    }

应该用这个更新:

private Dictionary<UInt16, string> cbSrcValueVals = new Dictionary<UInt16, string>();

事件被正确触发,但我找不到更新“绑定”组合框项目的方法。

private Dictionary<UInt16, string> cbDiagVals = new Dictionary<UInt16, string>();

感谢您的帮助。

此处编辑的是屏幕图像,其中包含ComboBox“Type”中的项目:

enter image description here

编辑,为属性更改添加的代码:

private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DgLedTable.CurrentCell = new DataGridCellInfo(DgLedTable.SelectedIndex, DgLedTable.Columns[1]);
}

1 个答案:

答案 0 :(得分:1)

如果您创建了一个源属性并将“绑定”ItemsSource的{​​{1}}属性绑定到此属性,则可以将此属性设置为“类型”ComboBox中的新集合ComboBox's事件处理程序。

窗口 - 或者您的代码定义的任何类型 - 必须实现INotifyPropertyChanged接口才能使其工作:

SelectionChanged