【问题标题】:How to update programmatically the items source in an autogenerated datagrid combobox cell如何以编程方式更新自动生成的数据网格组合框单元格中的项目源
【发布时间】:2017-02-10 13:12:55
【问题描述】:

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

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

我需要更新另一个属性,它也是同一个对象中的索引。我使用此代码在 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;
    }

【问题讨论】:

  • 所以您想将“Binding”组合框的 ItemsSource 从“cbSrcValueVals”更改为其他集合,对吗?那你想用什么集合作为新的 ItemsSource?
  • 是的,如果组合框类型设置为 2,我需要用另一个字典更新“绑定”组合框:private Dictionary&lt;UInt16, string&gt; cbDiagVals = new Dictionary&lt;UInt16, string&gt;();

标签: c# wpf data-binding autogeneratecolumn


【解决方案1】:

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

窗口——或者你的代码定义的任何类型——必须实现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));
    }

    //...
}

【讨论】:

  • 好的,我忘了删除cbBinding.ItemsSource = cbSrcValueVals;code 行...每次都重置。感谢您的帮助和优雅的解决方案......以及耐心!
  • 我还有一个问题:重新打开窗口时如何设置“绑定”组合的来源。默认情况下,“绑定”组合框设置为第一个字典 cbSrcValueVals。
  • 这里删除额外的 cmets。 @Alexus,如果您还有其他问题,请点击 按钮和minimal reproducible example 提出问题。您可以在新问题中引用此问题,但在发布之前尝试检查相关帖子。
猜你喜欢
  • 2020-03-08
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
相关资源
最近更新 更多