0

我找不到我的问题的有效解决方案

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

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

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

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

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

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

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

谢谢您的帮助。

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

在此处输入图像描述

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

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

1 回答 1

1

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

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

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

    //...
}
于 2017-02-10T13:35:27.280 回答