Binding properties from two user controls which have the same parent
up vote
0
down vote
favorite
Hello everyone :D
I am currently developing an "XML editor" WPF application which contains two layouts:
- A menu bar where the user can select an XML path (File > Open),
- An editor panel which loads and displays XML content in a pre-defined format.
Here is the overall architecture :
The following lines only show relevant files, but let me know if you need more details. There is no model in my MVVM software in order to give you a minimum number of files.
In MainWindow.xaml:
<Window x:Class="Software.MainWindow"
xmlns:local="clr-namespace:Software"
xmlns:v="clr-namespace:Software.Views"
xmlns:vm="clr-namespace:Software.ViewModels"
...>
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<DockPanel>
<v:MenuBar x:Name="MenuBar" DockPanel.Dock="Top" Panel.ZIndex="1"/>
<v:Editor x:Name="Editor" HorizontalAlignment="Center"/>
</DockPanel>
</Grid>
</Window>
In MainWindow:
namespace Software
public partial class MainWindow : Window
public MainViewModel mainViewModel = new MainViewModel();
public MainWindow()
InitializeComponent();
MenuBar.DataContext = mainViewModel.MenuViewModel;
Editor.DataContext = mainViewModel.EditViewModel;
private void MenuItem_Click(object sender, RoutedEventArgs e)
mainViewModel.MenuViewModel.selectXmlFile();
In MainViewModel:
namespace Software.Views
public class MainViewModel
public MenuBarViewModel MenuViewModel;
public EditorViewModel EditViewModel;
public MainViewModel()
MenuViewModel = new MenuBarViewModel();
EditViewModel = new EditorViewModel();
In MenuBar.xaml:
<UserControl x:Class="SmsReader.Views.MenuBar"
xmlns:vm="clr-namespace:SmsReader.ViewModels"
...>
<UserControl.DataContext>
<vm:MenuBarViewModel />
</UserControl.DataContext>
<Menu>
<MenuItem Header="File" >
<MenuItem Header="Open" Click="MenuItem_Click"/>
<Separator Margin="10, 0"/>
<MenuItem Header="Other"/>
</MenuItem>
<MenuItem Header="Other"/>
</Menu>
</UserControl>
In MenuBar:
namespace Software.Views
public partial class MenuBar : UserControl
public MenuBar()
InitializeComponent();
private void MenuItem_Click(object sender, RoutedEventArgs e)
MenuXmlPath = ((MenuBarViewModel) DataContext).getXmlFile();
In MenuBarViewModel:
namespace Software.ViewModels
public class MenuBarViewModel : INotifyPropertyChanged
private string xmlPath;
public string XmlPath
get return xmlPath;
set xmlPath = value; Fire("XmlPath");
public void selectXmlFile()
*.xml
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
In EditorViewModel:
namespace Software.ViewModels
public class EditorViewModel : INotifyPropertyChanged
private string xmlSource;
public string XmlSource
get return xmlSource;
set xmlSource = value; Fire("XmlSource"); update(value);
public void update(string xmlSource)
// Load XML data and display into the editor panel
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
I would like to link the property "XmlSource" to "XmlPath". What is the most elegant way to implement this?
Any advice concerning the architecture will be welcomed btw!
Thank you in advance :)
c# wpf xaml binding
add a comment |
up vote
0
down vote
favorite
Hello everyone :D
I am currently developing an "XML editor" WPF application which contains two layouts:
- A menu bar where the user can select an XML path (File > Open),
- An editor panel which loads and displays XML content in a pre-defined format.
Here is the overall architecture :
The following lines only show relevant files, but let me know if you need more details. There is no model in my MVVM software in order to give you a minimum number of files.
In MainWindow.xaml:
<Window x:Class="Software.MainWindow"
xmlns:local="clr-namespace:Software"
xmlns:v="clr-namespace:Software.Views"
xmlns:vm="clr-namespace:Software.ViewModels"
...>
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<DockPanel>
<v:MenuBar x:Name="MenuBar" DockPanel.Dock="Top" Panel.ZIndex="1"/>
<v:Editor x:Name="Editor" HorizontalAlignment="Center"/>
</DockPanel>
</Grid>
</Window>
In MainWindow:
namespace Software
public partial class MainWindow : Window
public MainViewModel mainViewModel = new MainViewModel();
public MainWindow()
InitializeComponent();
MenuBar.DataContext = mainViewModel.MenuViewModel;
Editor.DataContext = mainViewModel.EditViewModel;
private void MenuItem_Click(object sender, RoutedEventArgs e)
mainViewModel.MenuViewModel.selectXmlFile();
In MainViewModel:
namespace Software.Views
public class MainViewModel
public MenuBarViewModel MenuViewModel;
public EditorViewModel EditViewModel;
public MainViewModel()
MenuViewModel = new MenuBarViewModel();
EditViewModel = new EditorViewModel();
In MenuBar.xaml:
<UserControl x:Class="SmsReader.Views.MenuBar"
xmlns:vm="clr-namespace:SmsReader.ViewModels"
...>
<UserControl.DataContext>
<vm:MenuBarViewModel />
</UserControl.DataContext>
<Menu>
<MenuItem Header="File" >
<MenuItem Header="Open" Click="MenuItem_Click"/>
<Separator Margin="10, 0"/>
<MenuItem Header="Other"/>
</MenuItem>
<MenuItem Header="Other"/>
</Menu>
</UserControl>
In MenuBar:
namespace Software.Views
public partial class MenuBar : UserControl
public MenuBar()
InitializeComponent();
private void MenuItem_Click(object sender, RoutedEventArgs e)
MenuXmlPath = ((MenuBarViewModel) DataContext).getXmlFile();
In MenuBarViewModel:
namespace Software.ViewModels
public class MenuBarViewModel : INotifyPropertyChanged
private string xmlPath;
public string XmlPath
get return xmlPath;
set xmlPath = value; Fire("XmlPath");
public void selectXmlFile()
*.xml
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
In EditorViewModel:
namespace Software.ViewModels
public class EditorViewModel : INotifyPropertyChanged
private string xmlSource;
public string XmlSource
get return xmlSource;
set xmlSource = value; Fire("XmlSource"); update(value);
public void update(string xmlSource)
// Load XML data and display into the editor panel
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
I would like to link the property "XmlSource" to "XmlPath". What is the most elegant way to implement this?
Any advice concerning the architecture will be welcomed btw!
Thank you in advance :)
c# wpf xaml binding
3
You should read a lot more about MVVM, INotifyPropertyChanged, ICommand and how binding works between view and viewmodel
– Sir Rufo
Nov 9 at 0:24
If two properties should be the same, you should consider making them the same instead of linking them. Like anXmlSourceViewModel
that contains theXmlSource
(orXmlPath
, just use one name). Then just place the same instance of this new class inside both, theMenuViewModel
andEditViewModel
. There are many other ways if you don't like this approach or if it doesn't really reflect your business logic. Sir Rufo is right, you should learn some more concepts. It's not really common to design aViewModel
as aDependencyObject
. UsingINotifyPropertyChanged
is much more common.
– grek40
Nov 9 at 7:15
Very much agree with @Sir Rufo, your entire architecture is wrong here and you'll save yourself a hell of a lot of headaches down the track switching to MVVM now while you still can.
– Mark Feldman
Nov 9 at 9:40
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hello everyone :D
I am currently developing an "XML editor" WPF application which contains two layouts:
- A menu bar where the user can select an XML path (File > Open),
- An editor panel which loads and displays XML content in a pre-defined format.
Here is the overall architecture :
The following lines only show relevant files, but let me know if you need more details. There is no model in my MVVM software in order to give you a minimum number of files.
In MainWindow.xaml:
<Window x:Class="Software.MainWindow"
xmlns:local="clr-namespace:Software"
xmlns:v="clr-namespace:Software.Views"
xmlns:vm="clr-namespace:Software.ViewModels"
...>
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<DockPanel>
<v:MenuBar x:Name="MenuBar" DockPanel.Dock="Top" Panel.ZIndex="1"/>
<v:Editor x:Name="Editor" HorizontalAlignment="Center"/>
</DockPanel>
</Grid>
</Window>
In MainWindow:
namespace Software
public partial class MainWindow : Window
public MainViewModel mainViewModel = new MainViewModel();
public MainWindow()
InitializeComponent();
MenuBar.DataContext = mainViewModel.MenuViewModel;
Editor.DataContext = mainViewModel.EditViewModel;
private void MenuItem_Click(object sender, RoutedEventArgs e)
mainViewModel.MenuViewModel.selectXmlFile();
In MainViewModel:
namespace Software.Views
public class MainViewModel
public MenuBarViewModel MenuViewModel;
public EditorViewModel EditViewModel;
public MainViewModel()
MenuViewModel = new MenuBarViewModel();
EditViewModel = new EditorViewModel();
In MenuBar.xaml:
<UserControl x:Class="SmsReader.Views.MenuBar"
xmlns:vm="clr-namespace:SmsReader.ViewModels"
...>
<UserControl.DataContext>
<vm:MenuBarViewModel />
</UserControl.DataContext>
<Menu>
<MenuItem Header="File" >
<MenuItem Header="Open" Click="MenuItem_Click"/>
<Separator Margin="10, 0"/>
<MenuItem Header="Other"/>
</MenuItem>
<MenuItem Header="Other"/>
</Menu>
</UserControl>
In MenuBar:
namespace Software.Views
public partial class MenuBar : UserControl
public MenuBar()
InitializeComponent();
private void MenuItem_Click(object sender, RoutedEventArgs e)
MenuXmlPath = ((MenuBarViewModel) DataContext).getXmlFile();
In MenuBarViewModel:
namespace Software.ViewModels
public class MenuBarViewModel : INotifyPropertyChanged
private string xmlPath;
public string XmlPath
get return xmlPath;
set xmlPath = value; Fire("XmlPath");
public void selectXmlFile()
*.xml
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
In EditorViewModel:
namespace Software.ViewModels
public class EditorViewModel : INotifyPropertyChanged
private string xmlSource;
public string XmlSource
get return xmlSource;
set xmlSource = value; Fire("XmlSource"); update(value);
public void update(string xmlSource)
// Load XML data and display into the editor panel
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
I would like to link the property "XmlSource" to "XmlPath". What is the most elegant way to implement this?
Any advice concerning the architecture will be welcomed btw!
Thank you in advance :)
c# wpf xaml binding
Hello everyone :D
I am currently developing an "XML editor" WPF application which contains two layouts:
- A menu bar where the user can select an XML path (File > Open),
- An editor panel which loads and displays XML content in a pre-defined format.
Here is the overall architecture :
The following lines only show relevant files, but let me know if you need more details. There is no model in my MVVM software in order to give you a minimum number of files.
In MainWindow.xaml:
<Window x:Class="Software.MainWindow"
xmlns:local="clr-namespace:Software"
xmlns:v="clr-namespace:Software.Views"
xmlns:vm="clr-namespace:Software.ViewModels"
...>
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<DockPanel>
<v:MenuBar x:Name="MenuBar" DockPanel.Dock="Top" Panel.ZIndex="1"/>
<v:Editor x:Name="Editor" HorizontalAlignment="Center"/>
</DockPanel>
</Grid>
</Window>
In MainWindow:
namespace Software
public partial class MainWindow : Window
public MainViewModel mainViewModel = new MainViewModel();
public MainWindow()
InitializeComponent();
MenuBar.DataContext = mainViewModel.MenuViewModel;
Editor.DataContext = mainViewModel.EditViewModel;
private void MenuItem_Click(object sender, RoutedEventArgs e)
mainViewModel.MenuViewModel.selectXmlFile();
In MainViewModel:
namespace Software.Views
public class MainViewModel
public MenuBarViewModel MenuViewModel;
public EditorViewModel EditViewModel;
public MainViewModel()
MenuViewModel = new MenuBarViewModel();
EditViewModel = new EditorViewModel();
In MenuBar.xaml:
<UserControl x:Class="SmsReader.Views.MenuBar"
xmlns:vm="clr-namespace:SmsReader.ViewModels"
...>
<UserControl.DataContext>
<vm:MenuBarViewModel />
</UserControl.DataContext>
<Menu>
<MenuItem Header="File" >
<MenuItem Header="Open" Click="MenuItem_Click"/>
<Separator Margin="10, 0"/>
<MenuItem Header="Other"/>
</MenuItem>
<MenuItem Header="Other"/>
</Menu>
</UserControl>
In MenuBar:
namespace Software.Views
public partial class MenuBar : UserControl
public MenuBar()
InitializeComponent();
private void MenuItem_Click(object sender, RoutedEventArgs e)
MenuXmlPath = ((MenuBarViewModel) DataContext).getXmlFile();
In MenuBarViewModel:
namespace Software.ViewModels
public class MenuBarViewModel : INotifyPropertyChanged
private string xmlPath;
public string XmlPath
get return xmlPath;
set xmlPath = value; Fire("XmlPath");
public void selectXmlFile()
*.xml
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
In EditorViewModel:
namespace Software.ViewModels
public class EditorViewModel : INotifyPropertyChanged
private string xmlSource;
public string XmlSource
get return xmlSource;
set xmlSource = value; Fire("XmlSource"); update(value);
public void update(string xmlSource)
// Load XML data and display into the editor panel
public event PropertyChangedEventHandler PropertyChanged;
protected void Fire(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
I would like to link the property "XmlSource" to "XmlPath". What is the most elegant way to implement this?
Any advice concerning the architecture will be welcomed btw!
Thank you in advance :)
c# wpf xaml binding
c# wpf xaml binding
edited Nov 15 at 9:09
asked Nov 9 at 0:07
Martin Rostagnat
13
13
3
You should read a lot more about MVVM, INotifyPropertyChanged, ICommand and how binding works between view and viewmodel
– Sir Rufo
Nov 9 at 0:24
If two properties should be the same, you should consider making them the same instead of linking them. Like anXmlSourceViewModel
that contains theXmlSource
(orXmlPath
, just use one name). Then just place the same instance of this new class inside both, theMenuViewModel
andEditViewModel
. There are many other ways if you don't like this approach or if it doesn't really reflect your business logic. Sir Rufo is right, you should learn some more concepts. It's not really common to design aViewModel
as aDependencyObject
. UsingINotifyPropertyChanged
is much more common.
– grek40
Nov 9 at 7:15
Very much agree with @Sir Rufo, your entire architecture is wrong here and you'll save yourself a hell of a lot of headaches down the track switching to MVVM now while you still can.
– Mark Feldman
Nov 9 at 9:40
add a comment |
3
You should read a lot more about MVVM, INotifyPropertyChanged, ICommand and how binding works between view and viewmodel
– Sir Rufo
Nov 9 at 0:24
If two properties should be the same, you should consider making them the same instead of linking them. Like anXmlSourceViewModel
that contains theXmlSource
(orXmlPath
, just use one name). Then just place the same instance of this new class inside both, theMenuViewModel
andEditViewModel
. There are many other ways if you don't like this approach or if it doesn't really reflect your business logic. Sir Rufo is right, you should learn some more concepts. It's not really common to design aViewModel
as aDependencyObject
. UsingINotifyPropertyChanged
is much more common.
– grek40
Nov 9 at 7:15
Very much agree with @Sir Rufo, your entire architecture is wrong here and you'll save yourself a hell of a lot of headaches down the track switching to MVVM now while you still can.
– Mark Feldman
Nov 9 at 9:40
3
3
You should read a lot more about MVVM, INotifyPropertyChanged, ICommand and how binding works between view and viewmodel
– Sir Rufo
Nov 9 at 0:24
You should read a lot more about MVVM, INotifyPropertyChanged, ICommand and how binding works between view and viewmodel
– Sir Rufo
Nov 9 at 0:24
If two properties should be the same, you should consider making them the same instead of linking them. Like an
XmlSourceViewModel
that contains the XmlSource
(or XmlPath
, just use one name). Then just place the same instance of this new class inside both, the MenuViewModel
and EditViewModel
. There are many other ways if you don't like this approach or if it doesn't really reflect your business logic. Sir Rufo is right, you should learn some more concepts. It's not really common to design a ViewModel
as a DependencyObject
. Using INotifyPropertyChanged
is much more common.– grek40
Nov 9 at 7:15
If two properties should be the same, you should consider making them the same instead of linking them. Like an
XmlSourceViewModel
that contains the XmlSource
(or XmlPath
, just use one name). Then just place the same instance of this new class inside both, the MenuViewModel
and EditViewModel
. There are many other ways if you don't like this approach or if it doesn't really reflect your business logic. Sir Rufo is right, you should learn some more concepts. It's not really common to design a ViewModel
as a DependencyObject
. Using INotifyPropertyChanged
is much more common.– grek40
Nov 9 at 7:15
Very much agree with @Sir Rufo, your entire architecture is wrong here and you'll save yourself a hell of a lot of headaches down the track switching to MVVM now while you still can.
– Mark Feldman
Nov 9 at 9:40
Very much agree with @Sir Rufo, your entire architecture is wrong here and you'll save yourself a hell of a lot of headaches down the track switching to MVVM now while you still can.
– Mark Feldman
Nov 9 at 9:40
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217994%2fbinding-properties-from-two-user-controls-which-have-the-same-parent%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
You should read a lot more about MVVM, INotifyPropertyChanged, ICommand and how binding works between view and viewmodel
– Sir Rufo
Nov 9 at 0:24
If two properties should be the same, you should consider making them the same instead of linking them. Like an
XmlSourceViewModel
that contains theXmlSource
(orXmlPath
, just use one name). Then just place the same instance of this new class inside both, theMenuViewModel
andEditViewModel
. There are many other ways if you don't like this approach or if it doesn't really reflect your business logic. Sir Rufo is right, you should learn some more concepts. It's not really common to design aViewModel
as aDependencyObject
. UsingINotifyPropertyChanged
is much more common.– grek40
Nov 9 at 7:15
Very much agree with @Sir Rufo, your entire architecture is wrong here and you'll save yourself a hell of a lot of headaches down the track switching to MVVM now while you still can.
– Mark Feldman
Nov 9 at 9:40