views:

1558

answers:

3

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.

+4  A: 

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>
Nir
@Nir +1I was playing around with this same solution, but I couldn't quite get it working -- I was missing the Binding on the content presenter's content property.
JMarsch
also consider this thread: http://stackoverflow.com/questions/1017421/access-like-data-navigation-in-wpfthis is the solution I finally implemented. Its not even necessary that the control contains other controls as I noticed.
MAD9
A: 

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.

Joe White
+1  A: 

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).

Greg D
Your answer is more exactly what I needed (I believe), but Nirs solution does more exactly what I described above. Since only one answer is possible I ticked his one. But I'm currently evaluating what a Items control can do and it seems that ist pretty much what I had in mind (with a few customizations). thanks!
MAD9