Solved

Adding a User Control to a Window all in the same Visual Studio project

Posted on 2009-07-13
6
392 Views
Last Modified: 2013-11-12
I just cannot work this out, I know it is very simple if you are familiar with WPF.... I have a window and a user control both in the same Visual Studio 2008 project. How do I add the user control as a child in Window?
--------------------------------------------------------------------------------
Window XAML
--------------------------------------------------------------------------------
<Window x:Class="TextBannerTestApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:TextBanner;assembly=TextBanner"
    Title="Window1" Height="400" Width="1000" Background="Red">
    <StackPanel>
        <StackPanel Orientation="Horizontal" Height="30">
            <Button x:Name="btn1" Content="Test 1" Click="btn1_Click" />
            <Button x:Name="btn2" Content="Test 2" Click="btn2_Click" />
            <Button x:Name="btn3" Content="Test 3" Click="btn3_Click"/>
            <Button x:Name="btn4" Content="Test 4" Click="btn4_Click"/>
        </StackPanel>
    </StackPanel>
</Window>
 
--------------------------------------------------------------------------------
User Control XAML
--------------------------------------------------------------------------------
<UserControl x:Class="TextBannerTestApplication.UserControlTextBanner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <Button Margin="61,117,71,141" Name="buttonTest">Test 1 2 3</Button>
    </Grid>
</UserControl>

Open in new window

0
Comment
Question by:edward_mellor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 14

Expert Comment

by:chinu1310
ID: 24842448
What do you mean by child of the window ? Do you want to define your user control inside the stack panel you have on your window ? Did I understand correct ?

If that is the case , I see you have already added reference to the user control in your window.
Just add this line after your btn4 to have it in your Window

<uc:UserControl x:Name="yourcontrol"></uc:UserControl >

0
 

Author Comment

by:edward_mellor
ID: 24843253
It is the user control UserControlTextBanner that I want add the the window. This user control is in the same project as the window.
0
 
LVL 14

Expert Comment

by:chinu1310
ID: 24844073
From the code you have provided I have created a sample project I am attaching it with this post. I have take freedom to change some properties like background and etc.

Let me know if this is what you are looking for.

Thanks
0
 
LVL 14

Accepted Solution

by:
chinu1310 earned 500 total points
ID: 24844155
Nevermind, the upload is not allowing for some files in project. I will copy the code here.

<Window x:Class="TextBannerTestApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:UC="clr-namespace:TextBannerTestApplication"
   Title="Window1" Height="400" Width="1000" Background="White">
    <StackPanel>
        <StackPanel Orientation="Horizontal" Height="300" Width="800"  VerticalAlignment="Stretch">
            <UC:UserControlTextBanner x:Name="MyBannerUserControl" Height="300" Width="300"></UC:UserControlTextBanner>
            
            <Button x:Name="btn1" Content="Test 1"  Height="30" />
            <Button x:Name="btn2" Content="Test 2" Height="30" />
            <Button x:Name="btn3" Content="Test 3" Height="30"/>
            <Button x:Name="btn4" Content="Test 4" Height="30"/>
        </StackPanel>
    </StackPanel>
 
</Window>
 
 
Usercontrol
 
<UserControl x:Class="TextBannerTestApplication.UserControlTextBanner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" Background="Black">
    <Grid>
        <Button Margin="61,117,71,141" Name="buttonTest">Test 1 2 3</Button>
    </Grid>
</UserControl>

Open in new window

0
 
LVL 14

Expert Comment

by:chinu1310
ID: 24916392
Did it help ?
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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