如何从另一个窗口的代码隐藏访问一个窗口的属性?

时间:2016-11-17 08:52:10

标签: c# wpf xaml mvvm

我有两个窗口的应用程序。一个叫Options。它有string属性path_to_pure,表示某个文件的目录。用户可以在运行时通过OpenFileDialog更改它。在另一个窗口Window2(这是一个单独的XAML文件)中,有一个方法接受该属性并将该文件读入该Window2的属性。 如果重要的话,两个窗口都实现了INotyfyPropertyChanged个接口。 问题是我不知道如何从path_to_pure代码隐藏文件访问Options窗口的属性Window2。那么,访问它的最正确的WPF方式是什么?

选项窗口XAML:

<Window x:Class="PVT_Simulator.Options"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PVT_Simulator"
        mc:Ignorable="d"
        Title="Global Options" Height="300" Width="300"
        x:Name="Options_Window">
    <StackPanel DataContext="{Binding ElementName=Options_Window}">
        <TextBlock>Path to pure:</TextBlock>
        <TextBlock Text="{Binding Path=path_to_pure, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" Foreground="Blue"/>
        <Button Name="btnPurePath" Click="btnPurePath_Click" Content="Change" HorizontalAlignment="Left"/>
    </StackPanel>
</Window>

选项窗口C#

public partial class Options : Window, INotifyPropertyChanged
{
    private string _path_to_pure;
    public string path_to_pure
    {
        get { return _path_to_pure; }
        set
        {
            if (_path_to_pure != value)
            {
                _path_to_pure = value;
                NotifyPropertyChanged("path_to_pure");
            }
        }
    }

    public Options()
    {

        InitializeComponent();
        path_to_pure = "C:/Users/1/Dropbox/PVT_simulator/C# version/Components Lib/General Pure.txt"; // a default value

    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }

    private void btnPurePath_Click(object sender, RoutedEventArgs e)
    {
        // creating a file dialog to change file direction
        OpenFileDialog fd = new OpenFileDialog();
        fd.Filter = "Text files (*.txt)|*.txt";
        if (fd.ShowDialog() == true)
        {
            path_to_pure = fd.FileName;
        }

    }

 }

Window2 C#

public Window2()
{
    InitializeComponent();
    // generating pure components names
    string path_to_pure = // I need to acces Options path_to_pure property here!
    string[][] data = System.IO.File.ReadLines(path_to_pure)
                        .Skip(2).Select(l => l.Split('\t')
                        .ToArray())
                        .ToArray();
}

我是WPF和MVVM概念的新手,所以我非常感谢任何一般建议。

1 个答案:

答案 0 :(得分:2)

正如Clemens在评论中已经提到的,您应该首先确保使用正确的MVVM概念。所以你的Options-View(你的选项窗口)和你的Window2-View都应该有各自的ViewModel(OptionsViewModel和Window2ViewModel ......缺少一个更好的名字)。 为了帮助您更快地开始,我建议您使用MVVM框架/库。那里有很多好的选择,我个人喜欢使用MVVMLight by GalaSoft。在那里你会发现许多有用的类,比如ViewModelBase-class,它已经实现了INotifyPropertyChanged或者Messenger,它允许你轻松地在ViewModels之间发送消息(例如你可以使用Messenger在ViewModels之间传递 path_to_pure )。

MVVMLight还提供项目模板,引导您“正确”地指导如何在Model,ViewModel和View之间组织代码。当然没有绝对的正确方法,但我发现在学习WPF / MVVM时它非常有用。