[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

WPF Visual Inheritance

Posted on 2011-05-12
8
Medium Priority
?
531 Views
Last Modified: 2012-05-11
I have a user control in WPF, i want to use this use control as container control for other controls.

I have a user control which is having a grid with 3 rows, i want two rows(first and third) to be completly uneditable like it does in normal flow but the middle row should be open for user to place any control they want to place.
0
Comment
Question by:dineshwins
  • 5
  • 3
8 Comments
 
LVL 11

Expert Comment

by:saragani
ID: 35745308
Hi, in this case you should either inherit ContentControl instead of UserControl,
or have a DependencyProperty called Content and have a ContentControl or ContentPresenter in that that second row.
0
 
LVL 11

Expert Comment

by:saragani
ID: 35745512
<UserControl x:Class="UserDefinedContentControl.MyContentControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Template>
        <ControlTemplate TargetType="{x:Type UserControl}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>

                <Border Background="Red" />
                <ContentPresenter Grid.Row="1"/>
                <Border Background="Blue"  Grid.Row="2"/>
            </Grid>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

[/code


Now you can do:
[code]
<Window x:Class="UserDefinedContentControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UserDefinedContentControl"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:MyContentControl>
            <Button>
                Hello World
            </Button>
        </local:MyContentControl>
    </Grid>
</Window>

Open in new window

0
 

Author Comment

by:dineshwins
ID: 35745697
Thanks for the support, but in this way we could have only one content control in our user control, but i want to have multiple content controls inside my user control, what should i have to do to achieve this?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:saragani
ID: 35745716
I was using here the content property.
Since you need several content propertied, then you need to add dependency properies that will accept your UI and show it in your user control in the right places
0
 

Author Comment

by:dineshwins
ID: 35746295
Will you please elaborate?
0
 
LVL 11

Expert Comment

by:saragani
ID: 35746314
You wrote: " want two rows(first and third) to be completly uneditable like it does in normal flow but the middle row should be open for user to place any control they want to place."

That's what I gave you... Now you ask for something else: "but in this way we could have only one content control in our user control, but i want to have multiple content controls inside my user control"


So I don't understand what you want.... But here is a solution:


<UserControl x:Class="UserDefinedContentControl.MyContentControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">


    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Border Background="Red" /> 
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ContentPresenter Grid.Column="0" Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=UserControl}, Path=Content1}"/>
            <ContentPresenter Grid.Column="1" Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=UserControl}, Path=Content2}"/>
            <ContentPresenter Grid.Column="2" Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=UserControl}, Path=Content3}"/>
        </Grid>
        <Border Background="Blue"  Grid.Row="2"/>
    </Grid>
</UserControl>

Open in new window



You can also have it like this:
(But you will get exception in the Visual Studio Designer)
<UserControl x:Class="UserDefinedContentControl.MyContentControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:local="clr-namespace:UserDefinedContentControl"
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Template>
        <ControlTemplate TargetType="local:MyContentControl">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>

                <Border Background="Red" />
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Grid.Column="0" Content="{TemplateBinding Content1}"/>
                    <ContentPresenter Grid.Column="1" Content="{TemplateBinding Content2}"/>
                    <ContentPresenter Grid.Column="2" Content="{TemplateBinding Content3}"/>
                </Grid>
                <Border Background="Blue"  Grid.Row="2"/>
            </Grid>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

Open in new window






And have the Window contain that UserControl:
<Window x:Class="UserDefinedContentControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UserDefinedContentControl"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:MyContentControl>
            <local:MyContentControl.Content1>
                <Button>
                    Content 1
                </Button>
            </local:MyContentControl.Content1>

            <local:MyContentControl.Content2>
                <Button>
                    Content 2
                </Button>
            </local:MyContentControl.Content2>

            <local:MyContentControl.Content3>
                <Button>
                    Content 3
                </Button>
            </local:MyContentControl.Content3>

        </local:MyContentControl>
    </Grid>
</Window>

Open in new window

0
 
LVL 11

Accepted Solution

by:
saragani earned 2000 total points
ID: 35746355
0
 

Author Closing Comment

by:dineshwins
ID: 35751967
Thanks Mate
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question