1

I want to create a custom control with 2 parameters to be databindable. The parameters are ExchangeCode and TickerCode

Currently, here is what I am trying (which doesn't work) :

This is my custom control

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

    public static readonly DependencyProperty ExchangeCodeProperty = DependencyProperty.Register
                (
                     "ExchangeCode",
                     typeof(string),
                     typeof(Graph),
                     new PropertyMetadata(string.Empty)
                );

    public string ExchangeCode
    {
        get { return (string)GetValue(ExchangeCodeProperty); }
        set { SetValue(ExchangeCodeProperty, value); }
    }

    public static readonly DependencyProperty TickerCodeProperty = DependencyProperty.Register
                (
                     "TickerCode",
                     typeof(string),
                     typeof(Graph),
                     new PropertyMetadata(string.Empty)
                );

    public string TickerCode
    {
        get { return (string)GetValue(TickerCodeProperty); }
        set { SetValue(TickerCodeProperty, value); }
    }
}

This is the XAML in an other control

<grph:Graph TickerCode="ILD" ExchangeCode="EPA"/>

For now I didn't really bind data, but basically at the end it will be something like "{Binding Panes.TickerCode}" and "{Binding Panes.ExchangeCode}" but my ViewModel is not fully finished and I want to test how the parameters are passed so far.

So doing this, my custom control is properly added to the other custom control that calls it, but no ExchangeCode nor TickerCode is passed.

What am I missing?

Thank you all in advance, your help will be greatly appreciated.

samuel guedon
  • 575
  • 1
  • 7
  • 21
  • When are you checking these values? It is entirely possible that you are checking them *before* they have been set. Add a `Loaded` event handler for the control and check the values in there. – Mike Eason Jun 13 '15 at 07:27
  • probably missing `DataContext` http://stackoverflow.com/q/1705322/3191896 – Jossef Harush Kadouri Jun 13 '15 at 07:32
  • @ Mike Eason It seems you were right. I added a button with nothing to do but to check what you were saying and there the values are set. I will add an event to manage the Loaded and do my stuff after the values are set. I will post the solution once found. – samuel guedon Jun 13 '15 at 08:02
  • Well actually it is not enough. I can pass static value directly typed in my XAML (like in my exemple) but not a binding. I try adding the `DataContext="{Binding RelativeSource={RelativeSource Self}}"` but it doesn't fix my issue. – samuel guedon Jun 13 '15 at 15:48

0 Answers0