0
votes

I have MultiSelectComboBox UserControl inside my Custom UserControl. I'd like to bind the SelectedItems Dependency Property (from the MSCB above) which is type of Dictionary to any of My ViewModel Property...

*MSBC means --> MultiSelectComboBox UserControl !!!!!

Code-Behind of the MSCB: Define DP:

    public static readonly DependencyProperty SelectedItemsProperty =
     DependencyProperty.Register("SelectedItems", typeof(Dictionary<string, object>), typeof    (MultiSelectComboBox), new FrameworkPropertyMetadata(null,
       new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));

Define SelectedItems Property in MSCB:

     public Dictionary<string, object> SelectedItems
    {
        get { return (Dictionary<string, object>)GetValue(SelectedItemsProperty); }
        set
        {
            SetValue(SelectedItemsProperty, value);
        }
    }

      private static void OnSelectedItemsChanged(DependencyObject d,      DependencyPropertyChangedEventArgs e)
    {

        MultiSelectComboBox ctrl = (MultiSelectComboBox)d;
        ctrl.SelectNodes();
        ctrl.SetText();
    }

Xaml of My Custum UserControl: Define the MSCB in the Xaml:

      <MultiSelectComboBox:MultiSelectComboBox  x:Name="WorkDay" 
        SelectedItems="{Binding SelectedItemsInViewModel}" 
        ItemsSource="{Binding WorkDays,Converter={StaticResource DataConverter}}"/> 

In My ViewModel class:

    private Dictionary<string, object> si= new Dictionary<string, object>();
    public Dictionary<string, object> SelectedItemsInViewModel
    {
        get { return si; }
        set 
        { 
            si = value;
            OnPropertyChanged("SelectedItemsInViewModel");
        }
    }

It looks like the OnEventChanged (of the DProperty inside MSBC) is fired only for the first initialization and then stop firing. I don't get any changes in my ViewModel property. I've set the data context to point to my ViewModel class and other bindings inside this CustomControl are working fine (like TextBoxes).

2
Do you create a new Dictionary every time when you call the setter of SelectedItemsInViewModel? Can you show how you update your view model?Thomas Hetzer

2 Answers

0
votes

Looks like your code is incomplete. If the idea is to implement a Multi selection combo box, then you will need to more than just defining a SelectedItems property - you will need populate it when a item is selected/un-selected.

About,

I don't get any changes in my ViewModel property

Are you updating (SetValue) this property from View also? Or is it other way round?

0
votes

The solution worked for me is to use value converter interface, also you were right about using SetValue(), I've used it but the SelectedItems dictionary didn't set when I change the SelectedItems Dictionary because the DP point to referential object and when the list changing nothing happen unless you change the DP address each time, so I've added this line: SelectedItems = SelectedItems;

      private void SetSelectedItems()
      {
      if (SelectedItems == null)
            SelectedItems = new Dictionary<string, object>();
        SelectedItems.Clear();
        foreach (Node node in _nodeList)
        {
            if (node.IsSelected && node.Title != "ALL")
            {
                if (this.ItemsSource.Count > 0)

                    SelectedItems.Add(node.Title, this.ItemsSource[node.Title]);
            }
        }
        SelectedItems = SelectedItems;  //ADDED THIS LINE SOLEVED The DP not changed Problem
       }

after that I've used IValueConverter Interface in my ViewModel To handle Dictionary and convert it to what I've needed....

Thanks for helping me out to get the solution , guess I saved few more hours...