3

チェックボックスをチェックまたはチェック解除すると、テキストボックスの名前を変更するプログラムを作成しました。この Textbox を別のウィンドウに複製したいと考えています。xaml でのデータ マイニングが可能だと思ったのですが、名前が 1 つのウィンドウにしか表示されません。2 番目のウィンドウ ウィンドウがデータを受信しません。2 つのウィンドウのコードを示します。手伝って頂けますか?ありがとう

ウィンドウ 1.cs---

namespace WpfApplication1
{

public partial class Window1 : Window
{
    Texto prueba = new Texto("Carlos");


    public static string s;
    public Window1()
    {
       InitializeComponent( );
      // Fill initial person fields
       this.textBox1.Text = prueba.Name;          

    }


    private void checkBox1_Checked(object sender, RoutedEventArgs e)
    {


        prueba.Name="Carlos";
        textBox1.DataContext = prueba;
        textBox1.Text = prueba.Name;
    }

    private void checkBox1_UnChecked(object sender, RoutedEventArgs e)
    {
        prueba.Name = "Luis";
        textBox1.DataContext = prueba;
        textBox1.Text = prueba.Name;
    }
}

 public class Texto
{
    string name;
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

     public Texto( ) {}
     public Texto(string name) 
     {
       this.name = name;
     }

}


}

window1 xaml-------

     <Grid>
    <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="62,118,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_UnChecked" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="44,140,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>

window2cs-----

 namespace WpfApplication1
 {

   public partial class MainWindow : Window
  {
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Window1 nueva = new Window1();
        nueva.Show();
    }
 }


}

window2 xaml--------

<Grid>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="82,121,0,0" Name="button1"  VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <TextBox DataContext="prueba" Text="{Binding Path=Name}" Height="23" HorizontalAlignment="Left" Margin="57,84,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
  </Grid>
4

4 に答える 4

4

最初のウィンドウまたはテキスト プロパティを更新しているオブジェクトへの参照を 2 番目のウィンドウに渡す必要があります。これは DataContext プロパティで行います。次に、2 番目のウィンドウ コントロールをそれにバインドできます。

このデモ アプリケーションでは、MainWindow と 2 つ目のウィンドウ (Window1) を作成しました。アプリケーションは、このようにメイン ウィンドウで起動します。

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public string TestString
    {
        get { return (string)GetValue(TestStringProperty); }
        set { SetValue(TestStringProperty, value); }
    }

    public static readonly DependencyProperty TestStringProperty =  DependencyProperty.Register("TestString", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();

        // setup the test string.
        TestString = "this is a test.";

        // Create the second window and pass this window as it's data context.
        Window1 newWindow = new Window1()
        {
            DataContext = this
        };
        newWindow.Show();
    }
}

MainWindow.xaml - Window 宣言の DataContext 行に注意してください。

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
    <Grid>
        <TextBox Text="{Binding TestString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="91,84,185,189" />
    </Grid>
</Window>

Window1 の場合、コード ビハインドは単なる空のデフォルト ウィンドウ クラスであるため、投稿しませんが、xaml は投稿します。

Window1.xaml

<Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBlock Text="{Binding TestString, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>
于 2012-05-31T15:35:13.413 に答える
1

DataContext を明示的に設定しないでください。または、別のバインディングを介してのみ設定してください。君の

<TextBox DataContext="prueba"

役に立ちません。DataContext は上書きされない限り継承されます。明示的に設定しないでください。両方のウィンドウで一度設定するだけで十分です。

MainWindow でデータ オブジェクトを作成します。

Texto prueba = new Texto("Carlos");
Window1 nueva = new Window1();
nueva.DataContext = prueba;
nueva.Show();

他のすべての DataContext 割り当てを削除します。

于 2012-05-31T15:36:09.483 に答える
0

ここにはいくつか問題がありますが、問題を解決するための迅速な解決策を提供できると思います。まず、ウィンドウ 2 の DataContext が正しく機能していません。window1 を表示する直前に、コード内で排他的に設定できます...

private void button1_Click(object sender, RoutedEventArgs e)
{
    Window1 nueva = new Window1();
    this.DataContext = nueva.prueba;
    nueva.Show();
}

次に、Texto クラスで INotifyPropertyChanged を起動する必要があります...

public class Texto : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    string name;
    public string Name
    {
        get { return this.name; }
        set 
        { 
            this.name = value; 
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

     public Texto( ) {}
     public Texto(string name) 
     {
        this.name = name;
     }

}
于 2012-05-31T15:35:00.623 に答える
0

両方のテキストボックスが共通のデータコンテキストを共有している場合、コードを必要とせずに「そのまま機能」します...

于 2012-05-31T15:35:20.650 に答える