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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15549654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:13:21  来源:igfitidea点击:

Access the main window datacontext from a new created window

c#wpfwindowdatacontext

提问by Enrico

In my MainWindow I create a new instance of a class containing different settings. After setting the parameters of the class, I set the datacontext = to that class.

在我的 MainWindow 中,我创建了一个包含不同设置的类的新实例。设置类的参数后,我将 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 的数据上下文。

    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)中的文本后数据上下文没有得到更新。

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 的新手,欢迎提出任何建议!

回答by odyss-jii

As I wrote in the comment, you need to bind the Textproperty of your TextBoxto the property of the DataContextwhich you want to update. Your XAML should thus be something like:

正如我在评论中所写,您需要将TextTextBox的属性绑定到DataContext您要更新的属性。因此,您的 XAML 应该类似于:

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

Note that I am binding the Textproperty of the TextBoxto the property CommSettingsof your DataContext. And your C#-code for the click event should be:

请注意,我将 的Text属性绑定TextBoxCommSettings您的DataContext. 你C#的点击事件代码应该是:

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

We only need to set the DataContexthere. Note that the DataContextis passed along to child elements, so the TextBoxwill have the same DataContextas its parent unless specifically set to something else.

我们只需要在DataContext这里设置。请注意,DataContext传递给子元素,因此除非专门设置为其他内容,否则TextBox将与DataContext其父元素相同。

回答by David

use static property:

使用静态属性:

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

In other file:

在其他文件中:

Demo.SomeSettings=....