首页 > 解决方案 > 动态绑定 DataGridComboBox 列

问题描述

我目前正在开发一个小型 wpf 组件,但从 2 天以来,我一直在与 DataGrid 中的一个奇怪问题作斗争。我尝试重新加载 DataGridComboBoxColumn 的 ItemSource。无法将 xaml 中的此列绑定到 ObservableList

我的 .xaml :

<DataGrid Grid.Row="3" Height="auto" Margin="0, 15, 0, 0"  x:Name="mapAttributesTable" ColumnWidth="Auto"
              SelectedIndex="0" ItemsSource="{Binding LinkedItems, Mode=TwoWay}" SelectedItem ="{Binding Path=CurrentColumnObject, Mode=TwoWay}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridComboBoxColumn x:Name="comboBox_TableFields" Header="TableFields" ItemsSource="{Binding Path=DataContext.Fields, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <EventSetter Event="SelectionChanged" Handler="ComboBoxColumn_SelectionChanged"/>
                        <Setter Property="IsEditable" Value="True" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
            <DataGridTextColumn Header="JmsSubClass" Binding="{Binding Path= JmsSubClass}"/>
            <DataGridTextColumn Header="JmsFieldName" Binding="{Binding Path=JmsFieldName}"/>
        </DataGrid.Columns>
    </DataGrid>

我背后的代码:

    public IMap SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (value != null)
            {
                _selectedItem = value;
                this.Items = new ObservableCollection<ILink>(SelectedItem.Links);
                OnPropertyChanged();
                this.Fields = new ObservableCollection<string>(SelectedDataMapItem.TableFields);
                OnPropertyChanged("Fields");
            }
        }
    }

    private ObservableCollection<ILink> _items = new ObservableCollection<ILink>();

    /// <summary>
    /// Holds each created entry
    /// </summary>
    public ObservableCollection<ILink> Items
    {
        get { return this._items; }
        set
        {
            if (value != null)
            {
                this._items= value;
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private ILink _currentColumnObject;

    /// <summary>
    /// 
    /// </summary>
    public ILink CurrentColumnObject
    {
        get { return _currentColumnObject; }
        set
        {
            if (value != null)
            {
                this._currentColumnObject = value;
                OnPropertyChanged();
            }
        }
    }

    private ObservableCollection<string> _fields = new ObservableCollection<string>();

    public ObservableCollection<string> Fields
    {
        get
        {
            return _fields;
        }
        set
        {
            if (value != null)
            {
                this._fields = value;
            }
        }
    }

如果任何人(开发人员)有一个想法,解决这个问题会很好。

此致

标签: wpfmvvmdynamicbindingdatagridcomboboxcolumn

解决方案


推荐阅读