How to update programmatically the items source in an autogenerated datagrid combobox cell

非 Y 不嫁゛ 提交于 2019-12-25 08:06:18

问题


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

回答1:


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

    //...
}


来源:https://stackoverflow.com/questions/42160283/how-to-update-programmatically-the-items-source-in-an-autogenerated-datagrid-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!