0

I want to bind my DataGridComboBoxColumn on a ObservableCollection<string> Values. For this I wrote this .xaml code:

<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.cs is the manager class of Filter.cs. It contains a ObservableCollection<Filter> FilterCollection{get;set;} and some methods like public void CreateFilter(){}. But this methods works. When I execute the method CreateFilter(); the DataGrid shows one more entry.

My Filter.cs code:

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
}

I can bind on all propertys except Values.

Now I used Snoop to check if there any ItemsSource errors. THere exists an error:

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')

Got anyone of you an idea?

1 Answer 1

1
ObservableCollection<string> Values { get; set; }

In this setter property, you are not notifying which property is changed

Try something like this

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

I am sure that is the problem with your code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.