WPF在Windows之间传递数据 [英] WPF passing data between windows

查看:72
本文介绍了WPF在Windows之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



你好数据绑定有问题。我可以在一个窗口内的控件之间绑定数据,但在多个窗口之间使用相同的数据是微不足道的。



这是一个非常简单的程序,希望你明白这个想法



Window1.xaml(可能是应用程序MainWindow)



Hi all

Hi have problem with databinding. I can bind data between controlls inside one window, but using same data between multiple windows is trivial.

Here is very simple program, hope you get the idea

Window1.xaml (could be the application MainWindow)

<window x:class="WPF_Template.Window1" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1. Able to be synchronized with window2" Height="145" Width="359">
    <grid>
        <stackpanel>
            <slider name="slider">
                Margin="4" Interval="1"
                TickFrequency="1"
                IsSnapToTickEnabled="True"
                Minimum="0" Maximum="100"/>
            <stackpanel orientation="Horizontal">
                <textblock width="Auto" horizontalalignment="Left">
                       VerticalAlignment="Center" Margin="4" 
                       Text="Gets and sets the value of the slider:" />
                <textbox width="40" horizontalalignment="Center" margin="4">
                 Text="{Binding 
                        ElementName=slider, 
                        Path=Value, 
                        Mode=TwoWay, 
                        UpdateSourceTrigger=PropertyChanged}" />
            </textbox></textblock></stackpanel>
            <Button Content="Show Window2" Click="Button_Click"></Button>
        </slider></stackpanel>
    </grid>
</window>





和Window1.xaml.cs





and Window1.xaml.cs

using System.Windows;

namespace WPF_Template
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window2 window2 = new Window2();
            window2.Show();
        }
    }
}





现在我们有window1,我们想要创建window2





Now we have window1 and we want to create window2

<window x:class="WPF_Template.Window2" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2. Always synchronized with window1" Height="126" Width="355">
    <grid>
        <stackpanel>
            <slider name="slider" margin="4" interval="1" tickfrequency="1">
                IsSnapToTickEnabled="True"
                Minimum="0" Maximum="100"/>
            <stackpanel orientation="Horizontal">
                <textblock width="Auto" horizontalalignment="Left">
                       VerticalAlignment="Center" Margin="4" 
                       Text="Gets and sets the value of the slider:" />
                <textbox width="40" horizontalalignment="Center" margin="4">
                 Text="{Binding ElementName=slider,Path=Value,Mode=TwoWay, 
                        UpdateSourceTrigger=PropertyChanged}" />
            </textbox></textblock></stackpanel>
            <Button Content="Close" Click="Button_Click"></Button>
        </slider></stackpanel>
    </grid>
</window>





和Window2.xaml.cs





and and Window2.xaml.cs

using System.Windows;

namespace WPF_Template
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}





所以我想做的就是Window1和window2彼此同步。如果您编译我的代码,您将看到数据在Windows中单独绑定,并且窗口之间没有交互。



请给我建议!



最佳记录



该死的,无法发布我的完整xaml码。希望你有想法:doh:



So the thing what I want to do is that Window1 and window2 are synchronized with each other. If you compile my code you will see that data is tied within windows individually and there is no interaction between windows.

Please give me advice!

Best recards

Damn, cannot post my full xaml- code. Hope you got idea anyway :doh:

推荐答案

让Window1和Window2使用相同的DataContext。这样就可以共享数据,如果一个窗口更新某些内容,它会立即反映在另一个窗口中。



它基本上是一个 View-Model ,两个查看 s。
Have Window1 and Window2 use the same DataContext. That way the data is shared, and if one window updates something, it's instantly reflected in the other one.

It'll basically be one View-Model, two Views.


WPF在打开的窗口之间传递数据的另一种方式。


MainWindow.xaml.cs中的


another way for WPF passing data between open windows.

in MainWindow.xaml.cs:
((Window1)Application.Current.Windows[1]).textBlock1.Text="your text";
((Window1)Application.Current.Windows[1]).myProperty1=12;






or

foreach (Window item in Application.Current.Windows)
{
   if (item.Name == "Window1")
   {
      (Window1)item).textBlock1.Text="your text";
      (Window1)item).myProperty1=12;
   }
}







引用:

textblock1控件和Window1.xaml中的myProperty1

textblock1 control and myProperty1 in Window1.xaml


这篇关于WPF在Windows之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆