问题描述
大家好
你好数据绑定有问题。我可以在一个窗口内的控件之间绑定数据,但在多个窗口之间使用相同的数据是微不足道的。
这是一个非常简单的程序,希望你明白这个想法
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 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)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之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!