Matt
Matt

Reputation: 7254

WPF bindable dependency property is not working

I bind as follows:

views:SciChartUserControl Name="SciChartUserControl" Quotes="{Binding QuoteCollection}"></views:SciChartUserControl>

I know for sure that QuoteCollection updates because a grid also binds to it and I see it updated.I want to be notified in the code-behind of my SciChartUserControl view but QuotesPropertyChanged is never invoked. This is driving me crazy, I have tried different ways for hours...something obvious I am overlooking?

public partial class SciChartUserControl : UserControl
{
    private SciChartControlViewModel _viewModel;

    public SciChartUserControl()
    {

        //Set ViewModel Datacontext
        _viewModel = new SciChartControlViewModel();
        DataContext = _viewModel;

        InitializeComponent();
    }

    public static DependencyProperty QuotesProperty = DependencyProperty.Register("Quotes", typeof(List<Quote>), typeof(SciChartUserControl), new PropertyMetadata(QuotesPropertyChanged));

    public List<Quote> Quotes
    {
        get
        {
            return (List<Quote>)GetValue(QuotesProperty);
        }

        set
        {
            SetValue(QuotesProperty, value);
        }
    }

    private static void QuotesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        throw new NotImplementedException();

        var quotes = (List<Quote>) e.NewValue;
    }



}

EDIT: I added part of the view that hosts the SciChartUserControl.

 <dxdo:LayoutPanel Caption="Time Series Visualization">

                        <views:SciChartUserControl Name="SciChartUserControl" Quotes="{Binding QuoteCollection}"></views:SciChartUserControl>

                    </dxdo:LayoutPanel>

                    <dxdo:LayoutPanel Caption="Time Series Data">
                        <dxg:GridControl Name="SampleDataGridControl" ItemsSource="{Binding QuoteCollection}" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" AutoGeneratedColumns="SampleDataGridControl_OnAutoGeneratedColumns">
                            <dxg:GridControl.View>
                                <dxg:TableView AllowEditing="False" AutoWidth="True" BestFitArea="All" AllowBestFit="True" ShowGroupPanel="True" ShowSearchPanelMode="Always"/>
                            </dxg:GridControl.View>
                        </dxg:GridControl>
                    </dxdo:LayoutPanel>

Upvotes: 1

Views: 136

Answers (3)

Derrick Moeller
Derrick Moeller

Reputation: 4960

I believe this is because you have set your DataContext in your code behind, I ran into the same issue when setting it in XAML? It seems as though a DependencyProperty is being bound relative to the DataContext of the UserControl. UserControl's DependencyProperty is null when UserControl has a DataContext

<views:SciChartUserControl Name="SciChartUserControl"
                           Quotes="{Binding DataContext.QuoteCollection, RelativeSource={RelativeSource AncestorType={x:Type dxdo:LayoutPanel}}}" />

Upvotes: 0

Sheridan
Sheridan

Reputation: 69987

Try using another constructor for the PropertyMetadata class:

public static DependencyProperty QuotesProperty = DependencyProperty.Register("Quotes", 
    typeof(List<Quote>), typeof(SciChartUserControl), 
    new PropertyMetadata(someDefaultvalue, QuotesPropertyChanged));

It could be that the single parameter constructor that takes a PropertyChangedCallback object that you are using is getting mixed up with the one that takes a single object parameter.

Upvotes: 1

Bathineni
Bathineni

Reputation: 3496

try this... in the dependency property declaration change PropertyMetadata to the following..

new PropertyMetadata(null, new PropertyChangedCallback(QuotesPropertyChanged))

Upvotes: 0

Related Questions