DataGrid DataGridComboBoxColumn ItemsSource绑定不起作用

时间:2016-08-18 06:13:05

标签: c# wpf datagrid

我想将DataGridComboBoxColumn绑定在ObservableCollection<string> Values上。为此,我写了这个.xaml代码:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding FilterMng.FilterCollection}" CanUserAddRows="False" SelectionChanged="ComboBox_SelectionChanged">
    <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Wert" Width="*" SelectedValueBinding="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged}">
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
                    <Setter Property="ItemsSource" Value="{Binding Values}"/>
                </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
                    <Setter Property="ItemsSource" Value="{Binding Values}"/>
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

FilterMng.csFilter.cs的经理类。它包含ObservableCollection<Filter> FilterCollection{get;set;}和一些方法,如public void CreateFilter(){}。但这种方法有效。当我执行方法CreateFilter();时,DataGrid会再显示一个条目。

我的Filter.cs代码:

public class Filter : INotifyPropertyChanged
{
    #region Properties

    ObservableCollection<string> Values { get; set; }

    private string _selectedProperty;
    public string SelectedProperty
    {
        get { return this._selectedProperty; }
        set { this._selectedProperty = value; this.OnPropertyChanged("SelectedProperty"); }
    }

    private string _selectedValue;
    public string SelectedValue
    {
        get { return this._selectedValue; }
        set { this._selectedValue = value; this.OnPropertyChanged("SelectedValue"); }
    }

    #endregion Properties

    #region c'tor

    public Filter()
    {
        if (this.Values == null)
            this.Values = new ObservableCollection<string>();

        Values.Add("Entry1");
    }

    #endregion c'tor

    #region OnPropertyChanged

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

    #endregion OnPropertyChanged
}

我可以绑定除Values之外的所有属性。

现在我使用Snoop检查是否有ItemsSource个错误。这是一个错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'Values' property not found on 'object' ''Filter' (HashCode=31703865)'. BindingExpression:Path=Values; DataItem='Filter' (HashCode=31703865); target element is 'TextBlockComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

你们有没有想法?

1 个答案:

答案 0 :(得分:1)

ObservableCollection<string> Values { get; set; }

在此setter属性中,您没有通知哪个属性已更改

尝试这样的事情

private ObservableCollection<string> values;
public ObservableCollection<string> Values { get { return values; } set {values = value; this.OnPropertyChanged("Values ");} }

我确信您的代码存在问题