UserControl Example

UserControls are fairly straightforward to work with. In both the Demo App and the WPF Contrib project, I show a little more sophisticated way of using them, mimicking something like Prism regions. Yet, there is nothing that says you have to do this, setting up ways to add and remove UserControls in a region via an Action Aggregator.

Instead, here I show the barebones usage: how to initialize a UserControl and add a Composite to it.

First, I add a small interface:
public interface IUserControlGuid
     string UserControlGuid { get; set; }
     void LoadUserControl();

Next, I add my UserControl class, implementing this interface:
public partial class MyUserControl : UserControl, IUserControlGuid
        private string _userCtlGuid = null;

        public string UserControlGuid
                return _userCtlGuid;

                _userCtlGuid = value;

        public MyUserControl()
            //InitializeComponent(); XAML removed. Initialize via code-behind.

        public void LoadUserControl()
            .SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3))

                .AddLabel<UserControl>(0, 0, "I am a User Control", Brushes.LightGreen)
                .EndComposite<UserControl, UserControlArgs>(null);     
}//end of class

Finally, I use this new UserControl class in my code:
string userCtlGuid = Guid.NewGuid().ToString();
MyUserControl usc = new MyUserControl();
usc.UserControlGuid = userCtlGuid;

There you go!

Last edited Jun 2, 2014 at 2:38 AM by stagathome0069, version 4


No comments yet.