I want to create a container usercontrol with design time support in WPF ? How can I do ?
If you want to have a container user control with design time support, you can create a ContentControl http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx to host other user controls inside your container control.
You can expose the content control as a public property and then assign any other user controls you create to this property to display within the ContentControl. You would have the design time support of the parent control or any child user controls by default since they would all just be user controls.
Declare the following in your UserControl at the top:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class ExpanderControl : UserControl
Required NameSpaces:
using System.ComponentModel;
using System.ComponentModel.Design;
Related
I'm working on a project that utilizes WPF, using the Prism library and Unity Container.
I have a mainwindow, which contains a mainviewmodel control which in turn is populated by a collection of user controls.
My goal is to set keyboard focus to the mainviewmodel where I have an EventTrigger InvokeCommandAction which reacts to keyeventsargs...
Currently the only way the command is fired if I use a textbox within the control (or child controls). My goal is to have the mainviewmodel control or grid get and preserve keyboard focus.
Any tips would be appreciated!
//Nathan
Either not understanding your question correctly or you should review the basic concepts of MVVM in a WPF implementation.
The View is the WPF controls.
WPF Window/UserControl files contain WPF markup which is the View.
Controls in a view leverage DataBindings to the DataContext property of either the control itself or the parent containing control (which it will inherit).
DataContext property is set to an instance of an object that is the ViewModel. It contains properties to hold values and commands to execute actions.
So conceptually there is no "mainviewmodel control", there is a MainView which contains controls and may in this case have its DataContext property set to an instance o MainViewModel. (hence my confusion)
Finally, while it is possible and some might even recommend writing UI rules/logic in a view model I haven't found much benefit in it.
You are much better off putting UI logic in the XAML or in the MinView code behind. You can still access the MainViewModel in the code behind by casting the MainView.DataContext property as a MainViewModel.
So for example:
MainView.KeyDown event can be wired up to call MainViewModel.CommandX.Execute();
I am working on wpf mvvm pattern. I have different user controls. Based on the checkbox selection, I want them to be loaded in the main screen (that is also a user control). I have one HomeViewModel class which I have been using to bind the user controls of my project. Can you help me with a suitable way?
You should have different ViewModels for each kinds of UserControl.
Create different DataTemplates for each ViewModel Types
Put a ContentControl with binding a property of HomeViewModel -
Value of property will be an instance of ViewModel ( UserControl's) and is set by toggling CheckBoxes.
In my WPF application, I have a usercontrol library. In this library, I have a listview control along with textbox and button controls.
I have placed this usercontrol in a WPF window form. How can I retrieve the listviewitems of my usercontrol in this WPF window form?
You ask a very general question. There are dozens of ways to do that, depends on your needs. For example: when you place you UserControl within the Window - you should name it (e.g. . Then, within the UserControl, you can have a public function, or a public getter, which will return the items (it is the 'Items' property of the ListView).
Another way is binding. If you have a ViewModel, or any other class as the DataContext, you can bind the ItemsSource of the ListView to a collection on your ViewModel. Unless you change the DataContext of the UserControl - it will have the same DataContext as the main Window.
I have a property of type List<MyItems> with the DesignerSerializationVisibility(DesignerSerializationVisibility.Content) attribute in MyCustomControl class. This allows the collection to be properly serialized to the designer file.
How do I initialize this collection such as when the user drags MyCustomControl on the Form, 3 items are added automatically? Exactly how the standard TabControl does with TabPages.
I guess that a method like the ASP.NET CreateChildControls() should exist for WinForms to accomplish this.
Thanks.
I discovered you need to implement your own designer and override the InitializeNewComponent() method to create child controls and eventually the InitializeExistingComponent() to edit them.
I would like to create a Silverlight custom control using C# only, without any xaml.
Here is my work so far (stripped down to the bare minimum for the question):
I tried to inherit User control as follows:
public class myControl: UserControl
{
// class code
}
And adding it to the LayoutRoot:
myControl control = new myControl();
LayoutRoot.Children.Add(control);
The control is added, but its invisible!!
How can i make it visible ? Is there something i missed ?
edit: The only visual element in my contorl is a grid with an image background
Your Usercontrol will be empty and have no visual effect until you give it a child control via it's Content property.
Well unless you put a template in place or add elements in code, UserControl is empty.
Maybe you could try inheriting from an existing control which has a template, like Button, etc and change that in code?