tags:

views:

47

answers:

2

In my application I have some controls that logically belongs together and is reused many places in different windows. The controls are always placed inside a grid.

Instead of copying the controls (and the code behind) each time I want to use them, I would like to define and maintain them in a single xaml file as a single UserControl.

I have this now:

<Grid>
    <Grid.ColumnDefinitions>
        [ColumnDefinitions...]
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        [RowDefinitions...]
    </Grid.RowDefinitions>

    <StackPanel Grid.Column="0" Grid.Row="0">
        <TextBlock Text="Caption" />
        <Border Padding="2" x:Name="myBorder">
            <TextBox TabIndex="1" x:Name="myTxt"/>
        </Border>
    </StackPanel>
    <ListBox x:Name="myList" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Margin="5,50,5,0" Height="100" VerticalAlignment="Top" Visibility="Collapsed" />

    [More controls..]
</Grid>

But I want to reuse this part:

    <StackPanel Grid.Column="0" Grid.Row="0">
        <TextBlock Text="Caption" />
        <Border Padding="2" x:Name="myBorder">
            <TextBox TabIndex="1" x:Name="myTxt"/>
        </Border>
    </StackPanel>
    <ListBox x:Name="myList" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Margin="5,50,5,0" Height="100" VerticalAlignment="Top" Visibility="Collapsed" />

as a single control - but how do I define the Grid.Column when using the control (somehow supplying it as a parameter)? - and how do I set the Grid.RowSpan value (eventhough the code is moved to a new xaml file, and not defined inside a grid)?

Any comments?

+1  A: 

Make them into a separate usercontrol, then include that in your project.

If you're using Blend, it's really easy, just select all the controls, right click and Make into Usercontrol.

Doobi
I am not using blend, but I'll look into creating a seperate user control manually (in VS 2008). Further comments or ideas would be appreciated.
pcoltau
A: 

You could also make it into a resource. Define it in a ResourceDictionary and include the dictionary the places you want to use it. There is one catch - the resource dictionary returns the same instance everytime - so you have to add the attribute x:Shared="false".

But the wpf way is to figure out how you can do it with a DataTemplate :)

Rune Andersen
Thank you for your reply. I will look into DataTemplates ;-) Any links to good run-throughs?
pcoltau
I found many on google - this seems to be one of the better: http://www.code-magazine.com/Article.aspx?quickid=0907111
Rune Andersen
Thank you for the link. It more or less covers my basic knowledge of DataTemplates. Right now, my problem boils down the the fact that my newly created DataTemplate must contain two controls (StackPanel and ListBox) and that is not possible ("A template can have only a single root element"). But I can not just wrap the two controls in a grid inside the DataTemplate - that would make the Grid.RowSpan="2" of the ListBox apply to my "local" grid, and not the "parent" grid, that my custom UserControl is intended to by placed inside (does that make sense?). I am not sure how this could be solved?
pcoltau
I have tried to add "DataType="{x:Type Grid}"" to the DataTemplate, but that does not solve "...can have only a single root element"...?
pcoltau