【问题标题】:DataGrid DataGridComboBoxColumn ItemsSource Binding not workingDataGrid DataGridComboBoxColumn ItemsSource 绑定不起作用
【发布时间】:2016-08-18 06:13:05
【问题描述】:

我想将我的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&lt;Filter&gt; 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')

你们有什么想法吗?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:
    ObservableCollection<string> Values { get; set; }
    

    在此 setter 属性中,您不会通知更改了哪个属性

    试试这样的

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

    我确定这是您的代码的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-12
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 2015-05-23
      • 2014-01-04
      • 2011-12-17
      相关资源
      最近更新 更多