WPF Datagrid DataGridComboBoxColumn cells displayed value set during the AutoGeneratingColumn event

梦想与她 提交于 2019-12-11 00:15:59

问题


I have a DataGrid ComboBox Column that is bound to an Observable Collection of ComboBoxOption objects, these objects just contain 2 values, one that is the value displayed to the user and the other the required value. I am setting the bindings etc. during the AutoGenerating Columns event and this is all working fine. The problem I am having is trying to make the combo box display the required value only. This cannot be done in the XAML (Unless I can set up a column template in the XAML and use it in the code behind) because the table columns etc are not defined in the XAML as the datagrid works with collections of different objects and the columns etc are configured during the Auto Generating Columns events. I have tried to set this up using the column cell style but I cant get it working. can anybody point me in the right direction

stripped down code:

private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    DataGridComboBoxColumn comboBoxColumn = new DataGridComboBoxColumn();
    comboBoxColumn.ItemsSource = (DataContext as vm_DataTable).DataGridComboBoxColumnOptions;   //// The item source is in the View Model (An Observable Collection of ComboBoxOptions)
    Binding comboBoxSelectionBinding = new Binding("ComboBoxSelectedItem");                     //// Bind the selected item to the ComboBoxOption "ComboBoxSelectedItem" Getter/Setter in the Model
    comboBoxSelectionBinding.Mode = BindingMode.TwoWay;
    comboBoxSelectionBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    comboBoxColumn.SelectedValueBinding = comboBoxSelectionBinding;
    e.Column = comboBoxColumn;

    Style comboBoxCellStyle = new Style { TargetType = typeof(DataGridCell) };
    comboBoxCellStyle.Setters.Add(new Setter(ComboBox.DisplayMemberPathProperty, "DisplayedValue"));
    e.Column.CellStyle = comboBoxCellStyle;
}

Solution: (I was pointed in the right direction by a post by @heliar)

ComboBoxOption Class:

class ComboBoxOption : INotifyPropertyChanged
{
    private string requiredValue = "";
    private string displayedValue = "";

    public string RequiredValue
    {
        get { return this.requiredValue; }
        set
        {
            this.requiredValue = value;
            OnPropertyChanged("requiredValue");
        }
    }

    public string DisplayedValue
    {
        get { return this.displayedValue; }
        set
        {
            this.displayedValue = value;
            OnPropertyChanged("displayedValue");
        }
    }

    public ComboBoxOption(string RequiredValue, string DisplayedValue)
    {
        this.requiredValue = RequiredValue;
        this.displayedValue = DisplayedValue;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

In the code behind for the view:

        private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            DataGridComboBoxColumn comboBoxColumn = new DataGridComboBoxColumn();
            comboBoxColumn.ItemsSource = (DataContext as vm_DataTable).DataGridComboBoxColumnOptions;   //// The item source is in the View Model/Data Context (An Observable Collection of ComboBoxOptions)
            Binding comboBoxSelectionBinding = new Binding("ComboBoxSelectedItem");                     //// Bind the selected item to the ComboBoxOption "ComboBoxSelectedItem" Getter/Setter in the Model
            comboBoxSelectionBinding.Mode = BindingMode.TwoWay;
            comboBoxSelectionBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            comboBoxColumn.SelectedValueBinding = comboBoxSelectionBinding;
            comboBoxColumn.SelectedValuePath = "RequiredValue";   // RequiredValue is the ComboBoxOption Getter/Setter for this field, (this is the value that the record contains for this column)
            comboBoxColumn.DisplayMemberPath = "DisplayedValue";  // DisplayedValue is the ComboBoxOption Getter/Setter for this field
            e.Column = comboBoxColumn;
         }

not sure the selected value binding would be required in most cases, but in my case I sometimes need access to both the displayed and required values :)

来源:https://stackoverflow.com/questions/45349465/wpf-datagrid-datagridcomboboxcolumn-cells-displayed-value-set-during-the-autogen

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