如何将文本框值传递给子页面文本框

时间:2013-10-27 09:19:02

标签: c# wpf data-binding

(全部在wpf中)

我有一个包含用户名和密码的登录窗口。当用户登录时,用户名的值将传递到MainWindow中的另一个username.text文本框。我通过在表单声明中传递一个字符串值来成功完成。你通常会在winforms中采用相同的方式。

现在,我访问子页面的方式是通过链接。

例如:Link DisplayName =“Home”Source =“/ Pages / Home.xaml”/>所以子页面内容显示在主窗口上。好像主窗口是某种框架。

我现在需要的是,MainWindow中的这个username.text将其值传递给子页面中的username.text文本框。

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

据我所知,最有效的方法是通过单向数据绑定。 现在这可以在同一页面中工作......但它不能在不同的页面和主窗口中工作。

    <TextBox x:Name="username" Text="{Binding Text, ElementName=alias, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Height="19" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11"/>

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

有没有人知道怎么做?

MainWindow的代码

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

<Window.DataContext>
    <TextBox x:Name="username" Text="{Binding Text, ElementName=alias, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Height="19" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11"/>
</Window.DataContext>

子页面的代码

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



<Grid HorizontalAlignment="Left" Height="780" Margin="10,10,0,0" VerticalAlignment="Top" Width="1260">

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


</Grid>

这是主窗口和子页面的xaml。

2 个答案:

答案 0 :(得分:1)

在你的视图模型中公开静态属性如下

public class ViewModel
{
  private static ViewModel instance=new ViewModel();

  public static Viewmodel Instance
  {
     get
     {
        return instance;
     }
  }
} 

在两个Xmal.cs中(构造函数中的MainWindow.xmal.cs和ChildPage.Xmal.cs文件设置数据上下文

    DataContext=Viewmodel.Instance

并在Xmal中更改TextBinding,如下所示

  <TextBox x:Name="username" Text="{Binding PropertyInViewModel, UpdateSourceTrigger=PropertyChanged}" Height="19" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11"/>

  <TextBox x:Name="alias" Text="{Binding PropertyInViewModel, UpdateSourceTrigger=PropertyChanged} Margin="186,64,0,0" Height="18" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" ></TextBox>

答案 1 :(得分:0)

您可以创建一个静态类

public static class Credentials
{
    public static string username { get; set;}
}

在登录期间分配它,然后您可以在基本VM类或您需要的每个clase中调用它。