用户控件和主窗口之间的关系 [英] Relations between UserControl and MainWindow

查看:88
本文介绍了用户控件和主窗口之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能让可能从用户控件和家长,主窗口(反之亦然访问数据/性能)?例如,假设我有一个文本框在我的主窗口名为 mainTextBox 。然后,我创建一个用户控件另一个文本框名为 ucTextBox 。我也有一个名为 ucButton 用户控件应该弹出一个的MessageBox 与值的乘积 mainTextBox.Text * ucTextBox.Text (转换为double,使其工作)。

How could I make possible to access data/properties from a UserControl and the parent, MainWindow (and viceversa)? For example, say I have a TextBox in my MainWindow called mainTextBox. Then I create a UserControl with another TextBox called ucTextBox. I also have a button called ucButton in the UserControl that should popup a MessageBox with the product of the values mainTextBox.Text * ucTextBox.Text (converted to double, to make it work).

我真正想知道的是如何实现动态地做到这一点,有一个按钮,允许创建更多用户控件是能够与家长互动。在这种情况下,它是没有意义的名字每用户控件

What I really want to know is how to achieve to do this dynamically, with a button that allows to create more UserControls that are capable to interact with the parent. In this case it makes no sense to name every UserControl.

我试过几件事情,主要是与获取,设置属性,但没有预期的结果。
我不知道我是否需要使用用户控件,但它似乎,我读过 CustomControl 是对于深度定制的,但我并不需要这一点。

I've tried several things, mainly with get,set properties but with no desired outcome. I'm not sure if I need to use UserControl, but it seems to, I've read that CustomControl is for a deep customization but I don't need that.

推荐答案

下面就是让你开始(什么可能被先生@Adriano的意思。)一个快速的示例:

Here is just a quick sample to get you started (and what probably was meant by mr. @Adriano):

RootViewModel.cs

public class RootViewModel :INotifyPropertyChanged
{
    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged = delegate {};

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    private double _x;
    private double _y;

    public double X
    {
        get { return _x; }
        set
        {
            _x = value;
            OnPropertyChanged("X");
        }
    }

    public double Y
    {
        get { return _y; }
        set
        {
            _y = value;
            OnPropertyChanged("Y");
        }
    }

    public double XY
    {
        get { return _x * _y; }
    }
}

UserControl1.xaml

<UserControl x:Class="WpfApplication2.UserControl1"
         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:DesignWidth="200">
<Grid>
    <GroupBox Header="User Control">
        <StackPanel>
            <Label Content="Y:" />
            <TextBox Text="{Binding Path=Y, UpdateSourceTrigger=PropertyChanged, FallbackValue=1}" Margin="5" />
            <Button Content="Press me" Click="OnButtonClick" />
        </StackPanel>
    </GroupBox>
</Grid>

UserControl1.xaml.cs

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void OnButtonClick(object sender, RoutedEventArgs e)
    {
        var viewModel = (RootViewModel)DataContext;
        var resultMessage = string.Format("{0} * {1} = {2}", viewModel.X, viewModel.Y, viewModel.XY);

        MessageBox.Show(resultMessage, "X * Y");
    }
}

MainWindow.xaml:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication21="clr-namespace:WpfApplication2"
    Title="Main Window" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <StackPanel>
        <Label Content="X:" />
        <TextBox Text="{Binding Path=X, UpdateSourceTrigger=PropertyChanged, FallbackValue=1}" Margin="5" Height="24" />
    </StackPanel>
    <WpfApplication21:UserControl1 Grid.Row="1" Margin="5" />
</Grid>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new RootViewModel
        {
            X = 5,
            Y = 7
        };
    }
}

这篇关于用户控件和主窗口之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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