Michael Niemand
Michael Niemand

Reputation: 1754

C# User Control that can contain other Controls (when using it)

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

What I'd like to do is the following: I want to create a user control that has a collection as property and buttons to navigate through this collection. I want to be able to bind this user control to a collection and display different controls on it (containing data from that collection). Like what you had in MS Access on the lower edge of a form ...

to be more precise:

When I actually use the control in my application (after I created it), I want to be able to add multiple controls to it (textboxes, labels etc) between the <myControly> and </mycontrol> If I do that now, the controls on my user control disappear.

Upvotes: 8

Views: 6356

Answers (4)

stricq
stricq

Reputation: 856

Here's a link to a built-in control (HeaderedContentControl) that does the same thing as the accepted answer except that it is an existing control in WPF since .Net 3.0

Upvotes: 0

Greg D
Greg D

Reputation: 44066

I'm having a hard time understanding your question, but I think what you're describing is an ItemsControl using DataTemplates to display the contents of (presumably) an ObservableCollection(T).

Upvotes: 1

Joe White
Joe White

Reputation: 97656

A UserControl may not be the best way to do this. You're wanting to add decorations around content, which is basically what Border does: it has a child element, and it adds its own stuff around the edges.

Look into the Decorator class, which Border descends from. If you make your own Border descendant, you should be easily able to do what you want. However, I believe this would require writing code, not XAML.

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

Upvotes: 0

Nir
Nir

Reputation: 29584

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

First, the code - 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 user control's XAML - 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>

And finally, the xaml to use 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>

Upvotes: 12

Related Questions