有什么方法我可以使用在MainPage中分配一个值并将其绑定在子窗口中

时间:2011-11-14 07:49:29

标签: c# silverlight xaml childwindow staticresource

我有一个Silverlight应用程序,其中我有一个MainPage,我需要在子窗口中分配变量Name并在不使用子对象的情况下分配它。我需要通过XAML将thise值绑定到Childwindow中的文本框。怎么办呢?

到目前为止,我所做的是在子窗口中使用依赖属性:

    nameProp = DependencyProperty.Register("strName", typeof(string), typeof(TestWindow), new PropertyMetadata(null, new PropertyChangedCallback(OnNameChange)));

    static TestWindow()
    {
        nameProp = DependencyProperty.Register("strName", typeof(string), typeof(TestWindow), new PropertyMetadata(null, new PropertyChangedCallback(OnNameChange)));
    }

    private static void OnNameChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        d.SetValue(nameProp, e.NewValue);
    }

    public string strName
    { 
        get {
        return (string)GetValue(nameProp);
        }

        set {
            SetValue(nameProp, value);
        }
    }

并在TestWindow XAML中尝试绑定它:

       <TextBox Text="{Binding Path=strName}" Height="23" HorizontalAlignment="Left" Margin="126,84,0,0" Name="txtName" VerticalAlignment="Top" Width="120"/>

如何从MainPage设置此dp的值。还是有更好的选择吗?

2 个答案:

答案 0 :(得分:0)

一种方法是:

  1. 将这些变量设为MainPage的公共属性。
  2. 将mainPage指定为您的子窗口DataContext。
  3. 在您的childWindow中通过XAML绑定到这些属性。

答案 1 :(得分:0)

我希望这会帮助你......你想要实现的目标......

ChildWindow Xaml文件:

     <controls:ChildWindow x:Class="ParentToChildWindow.ChildWindowControl"

           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

           Width="400" Height="300"

           Title="Pass Data from Parent to ChildWindow">

    <Grid x:Name="LayoutRoot" Margin="2">

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Orientation="Vertical">

            <TextBlock x:Name="txtValue" />

            <TextBlock x:Name="txtName"/>

        </StackPanel>

        <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75"

                Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />

        <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23"

                HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />

    </Grid>

</controls:ChildWindow>

ChildWindow CS文件:

namespace ParentToChildWindow    
{

    using System.Windows;   
    using System.Windows.Controls;

    public partial class ChildWindowControl : ChildWindow    
    {

        public int Value { get; set; }    
        public string Name { get; set; }

        public ChildWindowControl()    
        {   
            InitializeComponent();

        }    
        private void OKButton_Click(object sender, RoutedEventArgs e)    
        {

            this.txtValue.Text = this.Value.ToString();    
            this.txtName.Text = this.Name;    
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)    
        {
                this.DialogResult = false;

        }

    }

}

父CS文件:我已向父XAML添加了一个按钮,并添加了一个单击事件

 private void HandleButtonClickEvent(object sender, RoutedEventArgs e)    
 {

   ChildWindowControl childControl = new ChildWindowControl();    
   childControl.Value = 10;
   childControl.Name = "Data From Parent XAML to ChildWindow";    
   childControl.Show();

 }

传递theChildWindow

的构造函数中的值

为此我们创建ChildWindow的新实例的位置,我们需要将所需的值传递给构造函数。但请记住,ChildWindow控件中必须存在匹配的构造函数。

public ChildWindowControl(int value, string name)    
{

   InitializeComponent();    
   this.Value = value;    
   this.Name = name;

 }

这就是将数据从父XAML传递到ChildWindow

所需的全部内容