0

私は子ウィンドウに変数 Name を割り当て、子のオブジェクトを使用せずに割り当てる必要がある MainPage がある Silverlight アプリケーションを持っています。この値を XAML を介して 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 の値を設定するにはどうすればよいですか。または、より良い代替手段はありますか?

4

2 に答える 2

0

1つの方法は次のとおりです。

  1. これらの変数を MainPage のパブリック プロパティにします。
  2. mainPage を子ウィンドウの DataContext に割り当てます。
  3. childWindow で XAML を介してこれらのプロパティにバインドします。
于 2011-11-14T10:23:49.060 に答える
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 にデータを渡すために必要な作業はこれだけです。

于 2011-11-14T10:26:53.970 に答える