C # User Control, which may contain other controls (when used)

I found something about this issue for ASP, but that didn't help me much ...

I would like to do the following: I want to create a user control that has a collection as a property and buttons to navigate this collection. I want to be able to bind this user control to a collection and display various controls on it (containing data from this collection). Like what you had in MS Access at the bottom of the form ...

more precisely:

When I use a control in my application (after it was created), I want to be able to add several controls to it (text fields, labels, etc.) between <myControly> and </mycontrol> If I do this, the elements The controls on my control will disappear.

+6
c # wpf user-controls
source share
4 answers

Here is an example of one way to do what you want:

Firstly, the code is UserControl1.xaml.cs

 public partial class UserControl1 : UserControl { public static readonly DependencyProperty MyContentProperty = DependencyProperty.Register("MyContent", typeof(object), typeof(UserControl1)); public UserControl1() { InitializeComponent(); } public object MyContent { get { return GetValue(MyContentProperty); } set { SetValue(MyContentProperty, value); } } } 

And the XAML user control is UserControl1.xaml

 <UserControl x:Class="InCtrl.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300" Name="MyCtrl"> <StackPanel> <Button Content="Up"/> <ContentPresenter Content="{Binding ElementName=MyCtrl, Path=MyContent}"/> <Button Content="Down"/> </StackPanel> </UserControl> 

Finally, xaml uses our wonderful new control:

 <Window x:Class="InCtrl.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:me="clr-namespace:InCtrl" Title="Window1" Height="300" Width="300"> <Grid> <me:UserControl1> <me:UserControl1.MyContent> <Button Content="Middle"/> </me:UserControl1.MyContent> </me:UserControl1> </Grid> </Window> 
+8
source share

It's hard for me to understand your question, but I think you are describing an ItemsControl using DataTemplates to display the contents of a (presumably) ObservableCollection (T) .

+1
source share

UserControl may not be the best way to do this. You want to add decorations around the content, which Border basically does: it has a child, and it adds its own material around the edges.

Take a look at the Decorator class that Border starts with. If you create your own borderline descendant, you should be able to do what you want. However, I believe that this will require writing code, not XAML.

You can still force UserControl to wrap the buttons at the bottom, just so you can use the visual constructor for part of the process. But Decorator would be a good way to glue pieces together and allow user-defined content.

0
source share

Here's a link with an inline control ( HeaderedContentControl ) that does the same as the accepted answer, except that it is an existing control in WPF with .Net 3.0

0
source share

All Articles