在窗口开始

时间:2017-02-13 11:27:18

标签: c# wpf combobox datagrid autogeneratecolumn

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

这个组合框更新了另一个属性,它也是同一个对象中的索引。

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

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

以下是代码:

  public partial class LedTableEditor : MetroWindow, INotifyPropertyChanged
  {
    private object _sender;
    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 System.Collections.IEnumerable _source;
    public System.Collections.IEnumerable Source
    {
        get { return _source; }
        set { _source = value; OnPropertyChanged(); }
    }


    public LedTableEditor(object senderParam)
    {
         InitializeComponent();
        _sender = senderParam;
        DgLedTable.ItemsSource = (_sender as MainWindow)._document.Config.ListeLedTable.ListeLedTable;

        cbTypeVals.Add(0, eLedType.ALERTE.ToString());
        cbTypeVals.Add(1, eLedType.DIAG.ToString());
        cbTypeVals.Add(2, eLedType.TRIGGER.ToString());

        ListeData = (_sender as MainWindow)._document.Config.ListeDataTable.ListeDataTable.Select(x => x.Name).ToList();
        if (ListeData.Find(x => x == "OFF") == null) ListeData.Insert(0, "OFF");
        foreach (string s in ListeData) if(s != "") cbSrcValueVals.Add((UInt16)ListeData.IndexOf(s), s);

        ListeDiag = (_sender as MainWindow)._document.Config.ListeDiagTable.ListeDiagTable.Select(x => x.Name).ToList();
        if (ListeDiag.Find(x => x == "OFF") == null) ListeDiag.Insert(0, "OFF");
        foreach (string s in ListeDiag) if (s != "") cbDiagVals.Add((UInt16)ListeDiag.IndexOf(s), s);

        if (this.DgLedTable.Items.Count > maxLeds) this.DgLedTable.CanUserAddRows = false;
        DgLedTable.Visibility = Visibility.Visible;

        _source = cbSrcValueVals;
    }

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

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

当组合框“Type”SelectionChange事件触发时,我更新了Combobox“Binding”项目源:

private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if ((sender as ComboBox).SelectedIndex == 1)
       Source = cbDiagVals;
    else
       Source = cbSrcValueVals;
}

当我更改ComboBox“Type”选项时,一切正常,但是当我打开Led Table时,我需要更新每个组合框“Type”,默认情况下,“Source”在ctor中设置为cbSrcValueVals。

谢谢。

链接到原始问题:How to update programmatically the items source in an autogenerated datagrid combobox cell

0 个答案:

没有答案