关于WPF应用程序用户控件 [英] Regarding WPF Application user controls

查看:74
本文介绍了关于WPF应用程序用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,



我有两个用户控件。



1.Usercontrol1

2.Usercontrol2



我在这个主窗口中使用这两个Usercontrol就像这样。



mainwindow.xaml



在我的主窗口中,我正在使用像这样的Usercontrol1。

In My WPF application,

I am having two user controls.

1.Usercontrol1
2.Usercontrol2

And I am using these two Usercontrols in my mainwindow like this.

mainwindow.xaml

In my mainwindow I am using Usercontrol1 like this.

<my:Usercontrol1 Margin="0,0,0,17" Name="loginUserControl1" />



例如在我的Usercontrol中有一个字符串,一个按钮有button_click事件。



当用户点击按钮时,我们会像这样显示Usercontrol2。


for example In my Usercontrol having one string,one button having button_click event.

When ever user click on the button we are showing the Usercontrol2 like this.

private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
this.content=new Usercontrol2();
}



到目前为止工作正常。



我的问题是usercontrol2何时加载我需要将我的Usercontrol1字符串值传递给usercontrol2。



Usercontrol2依赖于usercontrol1。



我怎样才能做到这一点。



请与我分享解决方案。


Up to here working fine.

My question is when the usercontrol2 is loading I need pass my Usercontrol1 string value to the usercontrol2.

Usercontrol2 is dependent on the usercontrol1.

How can I achieve this.

Please share me the solution.

推荐答案

在你的Click事件中你可以访问userControl1中的字符串值(因为此代码在UserControl1中)



所以如果你向UserControl2添加一个名为myStringValue的属性



你可以稍微改变你的代码:

In your Click event you have access to the string value from userControl1 (because this code is in UserControl1)

So if you add a property to UserControl2 called myStringValue

you can change your code slightly to:
var uc = new UserControl2();
uc.myStringValue = this.yourTextBoxName.Text;
this.content = uc;


这篇关于关于WPF应用程序用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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