WPF Datagrid DataGridComboBoxColumn单元格显示AutoGeneratingColumn事件期间设置的值

时间:2017-07-27 11:26:41

标签: wpf datagridcomboboxcolumn

我有一个绑定到ComboBoxOption对象的Observable集合的DataGrid ComboBox列,这些对象只包含2个值,一个是显示给用户的值,另一个是所需的值。我在AutoGenerating Columns事件期间设置绑定等,这一切都正常。我遇到的问题是尝试使组合框仅显示所需的值。 这不能在XAML中完成(除非我可以在XAML中设置列模板并在后面的代码中使用它),因为表格列等未在XAML中定义,因为数据网格使用不同对象和列的集合在自动生成列事件期间配置等。我试图使用列单元格样式设置它,但我不能让它工作。任何人都可以指出我正确的方向

剥离代码:

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

解决方案:(我被@heliar的帖子指向了正确的方向)

ComboBoxOption类:

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

在视图背后的代码中:

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

在大多数情况下不确定是否需要选定的值绑定,但在我的情况下,我有时需要访问显示和所需的值:)

0 个答案:

没有答案