locked
User Controls Values in Main page?

    Question

  • Hi All,

    Am Developing Windows Store App,

    My requirement is as follows.

    I have an USERCONTROL which has a list of images and also this user control has an option of add or delete images.

    Am calling this user control in my main page.

    i need to get the count of images in my "Main Page" once user adds or deletes the images in user contrl.

    Any Suggestion would be of great help.

    Thanks


    Arjun

    Wednesday, November 12, 2014 1:30 PM

Answers

  • Each time the user adds or deletes an image (clicks the Add or Delete Button) an event is raised. In the ButtonClick eventhandler you can update the DependencyProperty Count of the UserControl (which is bound to the outer Count property of the MainPage) with the current Count property of the image List.

    If the inner Count property is updated the outer Count is also update (Mode=twoway).

    If you use INotifyPropertyChanged for the outer Count property e.g. to display the result in MainPage, you can use the notifying mechanism (set; area) to call your own method.

    Torsten


    Thursday, November 13, 2014 4:54 PM

All replies

  • Hi Arjun,

    look at this sample:

    https://social.msdn.microsoft.com/Forums/en-US/7c8e67f8-d447-4edb-8e22-030df8b59344/binding-problem-with-usercontrol-parent-datacontext-access-failure-?forum=winappswithcsharp

    to make a correct binding between UserControl and MainPage and use instead of Text an Int to get the count value from your UserControl.

    Code:

    Define a DependencyProperty in your MyUserControl.xaml.cs :

    public static readonly DependencyProperty CountProperty =
        DependencyProperty.Register("Count", typeof(int), typeof(MyUserControl), new PropertyMetadata(0));

    public int Count
    {
        get { return (int)GetValue(CountProperty); }
        set { SetValue(CountProperty, value); }
    }

    Use in MainPage:

    int MainPageCount  (DataContext)

    bind it to the Count of UserControl:

    <local:MyUserControl Count="{Binding MainPageCount, Mode=TwoWay}"/>

    You can define MainPageCount with INotifyPropertyChanged (e.g. abstract class BindableBase) to be notified if the value is changed.

    Regards

    Torsten




    Wednesday, November 12, 2014 2:21 PM
  • Hi Torsten,

    Thanks For your Reply,

    Like you said i have created Dependency Property in My "Usercontrol.cs" page.

    And When user clicks on add/delete photo am checking the count of list.

    My doubt is how do i assign that list count in my Mainpage

     <uc:UcPhotos x:Name="UCPhotosUI" Count="{ Binding Count,Mode=TwoWay}"  />


    Use in MainPage: int MainPageCount (DataContext)Which Data Context?(My Main page is

    having "Viewmodel1" and User Control is Using Different ViewModel bind it to the Count of UserControl: <local:MyUserControl Count="{Binding MainPageCount, Mode=TwoWay}"/> You can define MainPageCount with INotifyPropertyChanged (e.g. abstract class BindableBase) to be notified if the value is changed.

    Am Not Getting This Part of Code.

    Thanks




    Arjun


    Thursday, November 13, 2014 1:34 PM
  • I use abstract class BindableBase :

    public class DataLogic : BindableBase
    {
        public int MainPageCount
        {
            get { return _MainPageCount; }
            set
            {
                SetProperty(ref MainPageCount, value);

                // DoSomething();

            }
        }
        private int _MainPageCount = 0;

        // More properties ...
    }

    in MainPage :

    DataLogic dataLogic = new DataLogic();

    this.DataContext = dataLogic;

        
    Thursday, November 13, 2014 1:51 PM
  • No This Wont Solve My Problem.

    Coz My main Page uses different Viewmodel and User Control Uses Different ViewModel.

    My Req:

    UserControl is having a list of photos with add and delete button in it.

    Once user add/delete photo i need to get that count in my main page.

    Thanks


    Arjun

    Thursday, November 13, 2014 4:13 PM
  • Each time the user adds or deletes an image (clicks the Add or Delete Button) an event is raised. In the ButtonClick eventhandler you can update the DependencyProperty Count of the UserControl (which is bound to the outer Count property of the MainPage) with the current Count property of the image List.

    If the inner Count property is updated the outer Count is also update (Mode=twoway).

    If you use INotifyPropertyChanged for the outer Count property e.g. to display the result in MainPage, you can use the notifying mechanism (set; area) to call your own method.

    Torsten


    Thursday, November 13, 2014 4:54 PM