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 :



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 :)










share|improve this question



















  • 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 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














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 :



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 :)










share|improve this question



















  • 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 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












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 :



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 :)










share|improve this question















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 :



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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 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







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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế