Reputation: 1330
I have an application with two windows. One is called Options
. It has a string
property path_to_pure
representing a directory to some file. It can be changed by the user during runtime through an OpenFileDialog
. In another window Window2
(which is a separate XAML file), a have a method that takes that property and reads that file into the property of that Window2
.
Both windows have INotyfyPropertyChanged
interfaces implemented, if that matters.
Problem is that I don't know how to access the property path_to_pure
of the Options
window from the Window2
code-behind file. So, what is the most correct WPF way to access it?
Options Window 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>
Options 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();
}
I am very new to WPF and MVVM concept, so I would very much appreciate any general advice as well.
Upvotes: 2
Views: 1186
Reputation: 114
As Clemens already mentioned in the comments, you should first make sure to use proper MVVM concepts. So your Options-View (your Options window) and your Window2-View each should have their respective ViewModels (OptionsViewModel and Window2ViewModel...in lack of a better name). To help you get started faster I suggest you use a MVVM framework / library. There are a lot of good options out there, personally I like to use MVVMLight by GalaSoft. There you will find many useful classes such as a ViewModelBase-class, which already implements INotifyPropertyChanged or a Messenger, which lets you send Messages between ViewModels easily (for example you can use the Messenger to pass path_to_pure between ViewModels).
MVVMLight also offers project templates, which lead you in the "right" direction on how to organize your code between the Model, the ViewModel and the View. Of course there's no absolute right way to do it, but I found it very helpful when learning WPF/MVVM.
Upvotes: 2