locked
Custom Control - ViewModel Communication XAML

    Question

  • Hi,

    I have a custom control which contains two Listview and a button control.

    The ObservableCollection List that has to be binded to the Listview  is manipulated in viewmodel which is a result of Server API request.


    I am maintaining a Observable collection inside the Custom Control.

    How I can pass the Observable collection from view model class to the Custom Control.

    Code Below represent my Custom Control.



     [TemplatePart(Name = "GroupList", Type = typeof(ListView))]
        [TemplatePart(Name = "DetailList", Type = typeof(ListView))]
        [TemplatePart(Name = "NavigateToGroupView", Type = typeof(Button))]
        [TemplateVisualState(GroupName = "ToggledState", Name = "Normal")]
        [TemplateVisualState(GroupName = "ToggledState", Name = "Toggled")]
        public sealed class NavigationControl : Control, INotifyPropertyChanged
        {
            private ListView _groupList;
            private ListView _detailList;
            private Button _navigationToGroupView;
            private bool _isToggled = true;
            private ObservableCollection<UserInfoDetails> _OrganizationUserCollectionListDetails;
            public ObservableCollection<UserInfoDetails> OrganizationUserCollectionListDetails
            {
                get
                {
                    return _OrganizationUserCollectionListDetails;
                }
                set
                {
                    _OrganizationUserCollectionListDetails = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("OrganizationUserCollectionList"));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged = delegate { };
            public NavigationControl()
            {
    
                this.DefaultStyleKey = typeof(NavigationControl);
            }
    
            protected override void OnApplyTemplate()
            {           
                _groupList = GetTemplateChild("GroupList") as ListView;
                _detailList = GetTemplateChild("DetailList") as ListView;
                _navigationToGroupView = GetTemplateChild("NavigateToGroupView") as Button;
    
                if (_groupList != null)
                {
                    _groupList.ItemsSource = OrganizationUserCollectionListDetails;
                    _groupList.SelectionChanged += _groupList_SelectionChanged;
                }
                if (_detailList != null)
                {
                    _detailList.SelectionChanged += _detailList_SelectionChanged;
                }
                if (_navigationToGroupView != null)
                {
                    _navigationToGroupView.Click += _navigationToGroupView_Click;
                }
    
    
                base.OnApplyTemplate();
            }
    
            void _navigationToGroupView_Click(object sender, RoutedEventArgs e)
            {
                _isToggled = !_isToggled;
                VisualStateManager.GoToState(this,
                   _isToggled ? "Normal" : "Toggled", true);
            }
            void _detailList_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
    
            }
    
            void _groupList_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                _isToggled = !_isToggled;
                VisualStateManager.GoToState(this,
                    _isToggled ? "Normal" : "Toggled", true);
    
            }
        }



    Tuesday, February 25, 2014 1:52 PM

Answers

  • Have you tried using a dependency property? Then in the xaml where you override the default template, set the property to your view models observable collection.

    I'm on my phone so I can't submit code, but you should be able to set this in xaml: something like your property equals binding your collection, assuming your data context is your view model.

    Wednesday, February 26, 2014 5:28 AM