0

I need some help!

I have build a custom control and added to it a DependencyProperty of type:

Dictionary<string,int>

and from the XAML where the control is held I do a data binding to bind to the Dictionary outside the control.

here are some code snippets: View model of the control who hold the custom control

private Dictionary<string, int> _wordsList;

public Dictionary<string, int> WordsList
{
    get
    {
        return _wordsList;
    }
    set
    {
        _wordsList = value;
        RaisePropertyChanged("WordsList");
    }
}

public WordsViewModel()
{
    //CalculateWordsDictionary returns a dictionary<string,int>
    WordsList = CalculateWordsDictionary(texts);
}

XAML:

<local:MyControl WordsList="{Binding Path=WordsList}" />

Code behind of the custom control:

public Dictionary<string, int> WordsList
{
    get { return (Dictionary<string, int>)GetValue(WordsListProperty); }
    set { SetValue(WordsListProperty, value); }
}

// Using a DependencyProperty as the backing store for WordsList.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty WordsListProperty =
    DependencyProperty.Register("WordsList", typeof(Dictionary<string, int>), typeof(MyControl), new PropertyMetadata(new Dictionary<string, int>()));

I've set a break point on the set of the DependencyProperty and it never reaches this point.

I just can't figure out why this isn't working... or maybe there is some other way to pass the dictionary to the control?

btw I am using MVVM Light

DHN
  • 4,807
  • 3
  • 31
  • 45
Jay Arbiv
  • 59
  • 7
  • Can you add Propertychanged callback for DependencyProperty and check, public static DependencyProperty FirstProperty = DependencyProperty.Register( "First", typeof(string), typeof(MyType), new FrameworkPropertyMetadata( false, new PropertyChangedCallback(OnFirstPropertyChanged))); private static void OnFirstPropertyChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e) { PropertyChangedEventHandler h = PropertyChanged; if (h != null) { h(sender, new PropertyChangedEventArgs("Second")); } } – Sivakumar Jul 30 '13 at 06:34
  • If I understand correctly I should try to catch the property changed event in the code behind of the control... I have tried to do that: (http://stackoverflow.com/questions/12798909/how-to-catch-a-property-changed-event-after-binding) with no luck – Jay Arbiv Jul 30 '13 at 07:23
  • then it should be problem with your binding. Define converter and place break point in your converter and make sure your binding don't have any issues. Also you can check the output window for binding errors – Sivakumar Jul 30 '13 at 07:33
  • 1
    The `WordsList` setter isn't called when the property is set in XAML and/or by a binding. See [here](http://msdn.microsoft.com/en-us/library/bb613563.aspx) for an explanation. You'll have to register a [PropertyChangedCallback](http://msdn.microsoft.com/en-us/library/system.windows.propertychangedcallback.aspx) by property metadata. – Clemens Jul 30 '13 at 08:12
  • I build a converter for the binding and it doesn't reach it... I've checked for binding errors and no error related to that binding – Jay Arbiv Jul 30 '13 at 08:12
  • 1
    Is the `DataContext` of `MyControl` set anywhere? – Clemens Jul 30 '13 at 08:16
  • And please also note that you must not use `new Dictionary()` as default property value, as that would result in all MyControl instances using the same dictionary instance. – Clemens Jul 30 '13 at 08:26
  • I've tried to register a callback `public static readonly DependencyProperty WordsListProperty = DependencyProperty.Register("WordsList", typeof(Dictionary), typeof(WPFTagCloud), new FrameworkPropertyMetadata(new Dictionary(),FrameworkPropertyMetadataOptions.AffectsRender,new PropertyChangedCallback(WordListChanged)));` and it doesn't reach the callback function. I just can't figure out what is wrong! – Jay Arbiv Jul 30 '13 at 08:28
  • replaced the default value to null and there was a problem with the datacontext but it still not working now the datacontext is set on the code behind of the custom control `this.DataContext = this;` – Jay Arbiv Jul 30 '13 at 08:38

2 Answers2

1

the important stuff you did not post:) whats your binding within your usercontrol? you have to use some kind of "local binding" so that your MyControl binds to it dependency property. you can use ElementName binding like this:

EDIT: here is the code for the UserControl called MyControl (just a snippet)

 <MyControl x:Name=uc>
   <ContentControl Content="{Binding ElementName=uc, Path=WordsList}"/>
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • What I posted was the actual definition of the custom control in the xaml of the user control it is held in. The 'local' just the namespace of the custom control so I can access it through the xaml. I've now changed the names so it would be clearer: – Jay Arbiv Jul 30 '13 at 07:16
  • you did not understand me, the important code is the xaml definition of your usercontrol. that what my snippet is for. and because i dont know where the Wordslist is bind to in your usercontrol, i bind it to a ContentControl. your binding TO your usercontrol is right, but thats not the problem. at least you sould tell us what you wanna achieve with your dependency property – blindmeis Jul 30 '13 at 09:28
  • I have a user control that holds a custom control in his xaml. I wanna build a custom tag cloud control that gets through the dependency property a Dictionary (the word and its occurrences number). So I didn't really understand what yo suggest to fix this problem. – Jay Arbiv Jul 30 '13 at 09:48
  • then simply change my contentcontrol to your Cloudcontrol. but if you use dependency properties within usercontrols and if you want bind to it within your usercontrol, then you have to use ElementName Binding or something similar. here some more information http://stackoverflow.com/questions/12404337/binding-between-my-usercontrol-and-viewmodel or http://stackoverflow.com/questions/11226843/data-binding-in-wpf-user-controls – blindmeis Jul 30 '13 at 10:27
0

Fixed!!!!!

All the comments together was the question

I there was a DataContext defined, I removed it completely. Also I registered the property changed callback

worked as charm!

Thanks to all

Jay Arbiv
  • 59
  • 7