views:

36

answers:

2

Hi all,

We have a lot of controls that consist of: A container with rounded borders and a couple of buttons that call save & cancel commands on the view model somthing like this:

     <Border Background="White" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Opacity="1" Padding="5,5,5,5" VerticalAlignment="Center" HorizontalAlignment="Center">
        <StackPanel>
           <!--Some Control Stuff Here...-->

          <controls:SaveCancelButtons/>
        </StackPanel>
    </Border>

What I'd like to do is make a custom control/style/template etc that allows me to reuse this so I can just wrap any new user control in a set of tags that places its content into the stack panel (where the comment is above)

What's the best way of achiving this?

EDIT:

OK Now I have a template like so:

<ControlTemplate x:Key="RoundedBordersTemplate">
    <Border Background="White" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Opacity="1" Padding="5,5,5,5" VerticalAlignment="Center" HorizontalAlignment="Center">
        <StackPanel>
            <ContentPresenter/>

            <controls:SaveCancelButtons/>
        </StackPanel>
    </Border>
</ControlTemplate>

And the control is implimented like so:

<ContentControl Template="{StaticResource RoundedBordersTemplate}">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="Description: " Width="72"/>
                    <TextBox Text="{Binding Path=Description}"
                        Width="205" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="Type:" Width="72" />
                    <ComboBox ItemsSource="{Binding Path=TypeList}" 
                          DisplayMemberPath="Description" 
                          SelectedValuePath="ID"
                          Width="205" />
                </StackPanel>                
            </StackPanel>            
        </ContentControl>

But I only see the Save/Cancel buttons.

+1  A: 

Your control should inherit from ContentControl (Control that contains CONTENT - i.e. other controls). Like all proper WPF controls this control is TOTALLY INVISIBLE - and the other stuff (buttons visual appearance) is added via the templating / styling mechanism.

Your subclass of ContentControl is then responsible for actually providing the logic for the buttons.

TomTom
+3  A: 

I would suggest using a ControlTemplate for type ContentControl:

<ControlTemplate x:Key="RoundedBordersTemplate" TargetType="ContentControl">
        <Border Background="White" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Opacity="1" Padding="5,5,5,5" VerticalAlignment="Center" HorizontalAlignment="Center">
            <StackPanel>
                <ContentPresenter />
                <controls:SaveCancelButtons/>
            </StackPanel>
        </Border>
    </ControlTemplate>

Then use it as below:

<ContentControl Template="{StaticResource RoundedBordersTemplate}">
        <StackPanel>
            <Button>Hello</Button>
            <Button>World</Button>
        </StackPanel>
    </ContentControl>

To give you:

Example

Update: this has the advantage of not requiring you to subclass ContentControl for a View-only change

Steve Greatrex
jdoig
Without knowing details of the rest of your application it will be difficult to diagnose - it could be caused by a number of things. As a diagnostic step, can you see the content if you add them to the template instead of to the `ContentControl`?
Steve Greatrex
Thanks Steve, Yes I can see them if i add them to the template
jdoig
I think I'll need to see some sample code for this! Can you either add some to the current question or ask another?
Steve Greatrex
Cheers, code added to the original question.
jdoig