0

毫无疑问,我不是第一个提出这种问题的人。问题是对于其中的大多数,当涉及到 wpf 时,回复将参考 MVVM,并且没有真正解释它是如何完成的,或者至少给出一个适用的例子。其余的是特定于这些人的特殊情况。

想象一下,我在主窗口中有一个文本框,并且在页面中也有一个文本框。我需要将主窗口中文本框中的文本传递到子窗口中的文本框。那是怎么做的?

winforms 中使用的方法似乎只适用于窗口到窗口。每次在页面声明中输入值并运行它时,它都会引发异常。

页面是通过链接打开的,如果它有任何用途,我正在使用 wpf 的现代 ui 主题:http: //mui.codeplex.com

主窗口 xaml

  <mui:ModernWindow x:Class="Masca.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
Title="Masca Database Admin" Height="800" Width="1280" IsTitleVisible="True"
LogoData="F1 M 24.9015,43.0378L 25.0963,43.4298C 26.1685,49.5853 31.5377,54.2651 38,54.2651C 44.4623,54.2651 49.8315,49.5854 50.9037,43.4299L 51.0985,43.0379C 51.0985,40.7643 52.6921,39.2955 54.9656,39.2955C 56.9428,39.2955 58.1863,41.1792 58.5833,43.0379C 57.6384,52.7654 47.9756,61.75 38,61.75C 28.0244,61.75 18.3616,52.7654 17.4167,43.0378C 17.8137,41.1792 19.0572,39.2954 21.0344,39.2954C 23.3079,39.2954 24.9015,40.7643 24.9015,43.0378 Z M 26.7727,20.5833C 29.8731,20.5833 32.3864,23.0966 32.3864,26.197C 32.3864,29.2973 29.8731,31.8106 26.7727,31.8106C 23.6724,31.8106 21.1591,29.2973 21.1591,26.197C 21.1591,23.0966 23.6724,20.5833 26.7727,20.5833 Z M 49.2273,20.5833C 52.3276,20.5833 54.8409,23.0966 54.8409,26.197C 54.8409,29.2973 52.3276,31.8106 49.2273,31.8106C 46.127,31.8106 43.6136,29.2973 43.6136,26.197C 43.6136,23.0966 46.127,20.5833 49.2273,20.5833 Z"          
ContentSource="/Pages/Home.xaml">

子窗口 xaml

  <UserControl x:Class="Masca.Mail.Configuration"
     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" 
     xmlns:mui="http://firstfloorsoftware.com/ModernUI"
     mc:Ignorable="d" 
     d:DesignHeight="800" d:DesignWidth="1280">

<TextBox x:Name="alias" Margin="186,64,0,0" Height="18" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" ></TextBox>

我这样做是为了让程序知道哪个用户正在登录,并且可以记录谁对数据库进行了哪些更改。

4

1 回答 1

0

How I will do that is to set the DataContext property of second page to the first page. Then I can access the properties of first page in second page via Binding. For this its better to use MVVM in your application and then define the property in ViewModel of Page 1 and then define DataContext in page 2. Now you can access that property in page 2.

于 2013-10-27T12:59:26.750 回答