samuel guedon
samuel guedon

Reputation: 626

Binded property does not update in a custom control

I created a custom control with of property consisting in a list of a custom type (list<OHLCV>). I am using a dependency property to allow it to be bindable.

Here is my code behind

public partial class GraphControl : UserControl
{

    //OHLCVSerie Property

    public List<OHLCV> OHLCVSerie 
    { 
        get { return (List<OHLCV>)GetValue(OHLCVSerieProperty); }
        set { SetValueDP(OHLCVSerieProperty, value); }
    }

    public static readonly DependencyProperty OHLCVSerieProperty =
        DependencyProperty.Register("OHLCVSerie", typeof(List<OHLCV>), typeof(GraphControl), null);

    //reuse

    public event PropertyChangedEventHandler PropertyChanged;
    void SetValueDP(DependencyProperty property, object value,
        [System.Runtime.CompilerServices.CallerMemberName] String p = null)
    {
        SetValue(property, value);
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

    public GraphControl()
    {
        InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }
}

The XAML has not yet been modified (= the user control is empty, except for the code behind)

In my main window, I created an instance of my custom control, and I bound to it a list<OHLCV>

<Window x:Class="MarketAnalyzer.Tester.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:grph="clr-namespace:MarketAnalyzer.DataVisualization;assembly=MarketAnalyzer.DataVisualization"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <grph:GraphControl OHLCVSerie="{Binding OHLCVSerie}" Margin="0,41,0,0"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>

The code behind creates the list<OHLCV> at the initialization of the MainWindow, while the button modifies the list if clicked.

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// The OHLCV Serie
    /// </summary>
    private List<OHLCV> _ohlcvserie;
    public List<OHLCV> OHLCVSerie
    {
        get { return _ohlcvserie; }
        set
        {
            if (_ohlcvserie != value)
            {
                _ohlcvserie = value;
                RaisePropertyChanged("OHLCVSerie");
            }
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        OHLCVSerie = new List<OHLCV>();
        OHLCVSerie = CreateRandomOHLCV(1, new DateTime(2016, 1, 1, 9, 0, 0), 4000, 100);
        this.DataContext = new
        {
            OHLCVSerie,
        };
    }

    /// <summary>
    /// Generate a random serie following usual index distribution parameters
    /// </summary>
    /// <param name="MinuteStep">number of minutes between each tick</param>
    /// <param name="StartDate">starting date</param>
    /// <param name="StartValue">starting value at tick 0</param>
    /// <param name="N">Number of ticks</param>
    /// <returns></returns>
    public List<OHLCV> CreateRandomOHLCV(int MinuteStep, DateTime StartDate, double StartValue, int N)
    {
        List<OHLCV> RandomOHLCV = new List<OHLCV>();

        //whatever code that create my random list

        return RandomOHLCV;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        OHLCVSerie = CreateRandomOHLCV(1, new DateTime(2016, 1, 1, 9, 0, 0), 4000, 1000);
    }

If I go to check the value of my list in my custom control I see that it is correctly implemented at the initialization of the MainWindow (a value is passed, with 100 items in the list), but it is not updated when I click the button (still the same list with 100 items while the button click create a list with 1.000 items).

How can I have the list updated in my custom control when the corresponding list is changed in my MainWindow?

Upvotes: 0

Views: 63

Answers (1)

Giangregorio
Giangregorio

Reputation: 1510

You are setting your DataContext to the current instance of OHLCVSerie, try to set your DataContext to this (your MainWindow).

Upvotes: 1

Related Questions