Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Goal:
Reduce tight coupling and increase loose coupling for user control 1 and 2.

Problem:
Is it possible to send some instance from main windows to user control 1 and 2?
If yes, do you have a example or recommended link about it?

Information:
- I'm using VS 2013

Xaml code:
#
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:usercontrol_menu="clr-namespace:usercontrol_menu;assembly=usercontrol_menu" xmlns:usercontrol_kassa="clr-namespace:usercontrol_kassa;assembly=usercontrol_kassa" x:Class="main_system.MainWindow"
        Title="MainWindow" Height="900" Width="1300         ">
    <Grid>
        <Frame x:Name="mainFrame"/>
        <ContentControl x:Name="cc_content" Content="ContentControl" HorizontalAlignment="Left" Margin="482,144,-228,0" VerticalAlignment="Top" Height="298" Width="1038"/>
        <usercontrol_menu:UserControl1 HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <usercontrol_kassa:UserControl1 HorizontalAlignment="Left" Margin="10,344,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>
Posted
Comments
Matt T Heffron 7-May-14 13:07pm    
I'm unsure exactly what you are asking.
Are you trying to set some property of the MainWindow (or the MainWindow itself) to be the value of some property of the UserControls?
Please clarify what you want to accomplish. (Use the "Improve question" above.)
Sergey Alexandrovich Kryukov 7-May-14 15:27pm    
Yes, it is of course possible, but it would increase coupling to barely acceptable extend. Better invents something smarter than that. :-)
—SA

1 solution

Hi,

Just define the Grid regions and use "Place-Holders" (ContentPresenter):

XML
<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ContentPresenter x:Name="placeHolder1" Grid.Column="0" />
        <ContentPresenter x:Name="placeHolder2"  Grid.Column="1"/>
    </Grid>
</Window>

C#
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public void SetControlToPlaceHolder(E_Region region, UserControl control)
    {
        switch (region)
        {
            case E_Region.e_Place1:
                this.placeHolder1.Content = control;
                break;
            case E_Region.e_Place2:
                this.placeHolder2.Content = control;
                break;
        }
    }
}

public enum E_Region
{
    e_Place1 = 0,
    e_Place2 = 1,
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900