Michał Jankowski
Michał Jankowski

Reputation: 427

DependencyProperty ObservableCollection event

I am trying to make UserControl (GridSearch) which has another UserControl_2 inside. I want to add some FrameworkElements to panel of UserControl_2 using XAML.

So I did ObservableCollection DependencyProperty in GridSearch:

public partial class GridSearch : UserControl
{
    public GridSearch()
    {
        InitializeComponent();
    }

    public ObservableCollection<Filter> Filters
    {
        get { return (ObservableCollection<Filter>)GetValue(FiltersProperty); }
        set { SetValue(FiltersProperty, value); }
    }

    public static readonly DependencyProperty FiltersProperty =
        DependencyProperty.Register("Filters",
                                    typeof(ObservableCollection<Filter>), 
                                    typeof(GridSearch),
                                    new FrameworkPropertyMetadata(getObservableFilters(), null)
                                    );

    private static ObservableCollection<Filter> getObservableFilters()
    {
        var ob = new ObservableCollection<Filter>();
        ob.CollectionChanged += ob_CollectionChanged;
        return ob;
    }

    private static void ob_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {

    }
}

Now I was trying to add new elements to the panel by using ob_CollectionChanged. However because it is static method I cannot access the panel. I cannot cast the sender because it gives me only the ObservableCollection. However I need GridSearch.

I am looking for solution from several hours and I am not able to find any idea how to solve it.

Upvotes: 0

Views: 1124

Answers (2)

Michał Jankowski
Michał Jankowski

Reputation: 427

Ok it works finally the key thing was to create new ObservableCollection() in constructor for each instance of the control.

However there is still one problem. Everything works at runtime but designer is not able to display anything as it gets following error:

Object reference not set to an instance of an object.

and this is tabout this line: <gsh:GridSearch.Filters>

This is the code I end up with:

public partial class GridSearch : UserControl
{
    public GridSearch()
    {
        Filters = new ObservableCollection<Label>();
        InitializeComponent();


        Filters.CollectionChanged += Filters_CollectionChanged;
    }


    void Filters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        foreach (Label uc in e.NewItems)
            pnlFilters.Children.Add(uc);                
    }

    public ObservableCollection<Label> Filters
    {
        get { return (ObservableCollection<Label>)GetValue(FiltersProperty); }
        set { SetValue(FiltersProperty, value); }
    }

    public static readonly DependencyProperty FiltersProperty =
        DependencyProperty.Register("Filters",
                                    typeof(ObservableCollection<Label>), 
                                    typeof(GridSearch),
                                    new FrameworkPropertyMetadata(new ObservableCollection<Label>(), null)
                                    );



}

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.5*"/>
        <RowDefinition Height="0.5*"/>
    </Grid.RowDefinitions>

    <gsh:GridSearch>
        <gsh:GridSearch.Filters>
            <Label Content="aa" />
            <Label Content="aa" />
            <Label Content="aa" />
        </gsh:GridSearch.Filters>
    </gsh:GridSearch>

    <gsh:GridSearch Grid.Row="1">
        <gsh:GridSearch.Filters>
            <Label Content="bb" />
            <Label Content="cc" />
            <Label Content="dd" />
        </gsh:GridSearch.Filters>
    </gsh:GridSearch>
</Grid>

Upvotes: 0

Jogy
Jogy

Reputation: 2475

Change the getObservableFilters() method to just create and return the observable collection.

And in the GridSearch() constructor, after the call to InitializeComponent(), you can add a handler for Filters.CollectionChanged and provide a non-static member function.

Upvotes: 1

Related Questions