如何将实例从主窗口发送到用户控件 [英] How to Send Instance from Main Windows to User Control

查看:82
本文介绍了如何将实例从主窗口发送到用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

为用户控制1和2减少紧耦合并增加松耦合。



问题:

是否可以从主窗口向用户控件1和2发送一些实例?

如果是,您是否有关于它的示例或推荐链接?



信息:

- 我正在使用VS 2013



Xaml代码:

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>

推荐答案

您好,



只需定义网格区域并使用Place-Holders( ContentPresenter ):



Hi,

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

<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>




/// <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,
}


这篇关于如何将实例从主窗口发送到用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆