简体   繁体   English

从新创建的窗口访问主窗口数据上下文

[英]Access the main window datacontext from a new created window

In my MainWindow I create a new instance of a class containing different settings. 在我的MainWindow中,我创建一个包含不同设置的类的新实例。 After setting the parameters of the class, I set the datacontext = to that class. 在设置了类的参数之后,我将datacontext =设置为该类。

public partial class MainWindow : Window
{

 private MeasConSettings mMeasConSettings = new MeasConSettings();

  public MainWindow()
  {
    InitializeComponent();
    DataContext = mMeasConSettings;
  }

  private void MenuComm_Click(object sender, RoutedEventArgs e)
  {// See code below}

}

Now I also have a function to open a new window, this window contains a textbox who's text should be bound to the datacontext of the MainWindow. 现在,我还有一个打开新窗口的功能,该窗口包含一个文本框,该文本框的文本应绑定到MainWindow的datacontext。

    private void MenuComm_Click(object sender, RoutedEventArgs e)
    {
        FrmSettings newWindow = new FrmSettings();
        newWindow.DataContext = mMeasConSettings;
        newWindow.TxtComm.Text = mMeasConSettings.CommSettings;
        newWindow.Show();
    }

This code fills in the textbox from the newWindow with the right content, BUT it does not get bound propery since the datacontext does not get updated after changing the text in the textbox (TxtComm in the new created window). 这段代码使用正确的内容从newWindow填充了文本框,但是由于在更改文本框中的文本(新创建的窗口中的TxtComm)后datacontext没有得到更新,因此它没有得到正确的绑定。

An example of the XAML code for the textbox: 文本框的XAML代码示例:

<TextBox Grid.Row="1" Grid.Column="3" Margin="2,0"  Name="TxtComm" DataContext="{Binding Path=CommSettings, UpdateSourceTrigger=PropertyChanged}" />

"CommSettings" is a member of the MeasConsettings class “ CommSettings”是MeasConsettings类的成员

public class MeasConSettings
{
    private string mCommSettings;

    public string CommSettings
    {
        get
        {
            return mCommSettings;
        }
        set
        {
            mCommSettings = value;
        }
    }

    public MeasConSettings()
    {
        CommSettings = "Com5:19200,8,n,1";
    }
}

My problem is how can I adjust the value mMeasConSettings.CommSettings (defined in my MainWindow) in my newWindow (Which is created after pressing a button), If I change the textbox value in my newWindow, the value stored in mMeasConSettings.CommSettings should also be changed. 我的问题是如何在newWindow(按下按钮后创建)中调整值mMeasConSettings.CommSettings(在MainWindow中定义),如果更改了newWindow中的文本框值,则存储在mMeasConSettings.CommSettings中的值也应该被改变。

PS: I'm new to WPF so any advice is welcome! PS:我是WPF的新手,欢迎任何建议!

As I wrote in the comment, you need to bind the Text property of your TextBox to the property of the DataContext which you want to update. 正如我在评论中所写,您需要将TextBoxText属性绑定到要更新的DataContext的属性。 Your XAML should thus be something like: 因此,您的XAML应该是这样的:

<TextBox ... Text="{Binding CommSettings, Mode=TwoWay}" />

Note that I am binding the Text property of the TextBox to the property CommSettings of your DataContext . 注意,我将TextBoxText属性绑定到DataContext CommSettings属性。 And your C# -code for the click event should be: 而click事件的C#代码应为:

private void MenuComm_Click(object sender, RoutedEventArgs e)
{
    FrmSettings newWindow = new FrmSettings();
    newWindow.DataContext = mMeasConSettings;
    newWindow.Show();
}

We only need to set the DataContext here. 我们只需要在这里设置DataContext Note that the DataContext is passed along to child elements, so the TextBox will have the same DataContext as its parent unless specifically set to something else. 请注意, DataContext会传递给子元素,因此,除非专门设置为其他内容,否则TextBox将与其父元素具有相同的DataContext

use static property: 使用静态属性:

class Demo
{
    public static string SomeSettings {get;set;}
    private onLoad()
    {
        SomeSettings=... //Init here
    }
}

In other file: 在其他文件中:

Demo.SomeSettings=....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM