是否有某种方式,我可以使用在MainPage中分配一个值,并在子窗口中绑定它



我有一个Silverlight应用程序,其中我有一个MainPage,我需要在子窗口中分配一个变量名称,并在不使用子对象的情况下分配它。我需要通过XAML将这些值绑定到子窗口中的文本框。怎样才能做到呢?

到目前为止,我所做的是在子窗口中使用依赖属性:
    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的值?或者有更好的选择吗?

一种方法是:

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

我希望这将帮助您....你想要达到的目标....

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 file:

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

所需要的全部内容

最新更新