How can I split a large XAML file into XAML files and maintain parent-child communication?

I am new to WPF. I am trying to modify the VisualStudioLikePanes project from the WPF 4 Unleashed book. Since the panels are hidden by default until I run the project, I figured it would be a good idea to put the panel I'm working in in a separate xaml file so that I can see the changes I make in the panel without having to launch the executable.

So, based on some posts I read here on StackOverflow a few days ago, I added a new UserControl to the sample project and overlaid the contents of the panel in question. This is what the UserControl attributes look like in the "child" XAML file:

<UserControl x:Class="Sample.SettingsPanel"


To include this control in the parent, I added the xmlns: sp namespace to the parent XAML file:

    x:Uid="Window_1" x:Class="Sample.MainWindow"


Then I enabled control via this:

<sp:SettingsPanel Visibility="Collapsed" x:Name="layer1" x:FieldModifier="private" />


I immediately discovered that in the code file for the "parent" XAML file, all code that referenced any of the elements now contained in the "child" XAML file is now unrecognized. So I then removed (or commented out) all the name and object references that were now contained in the "child" XAML file, and have since jumped through the hoops to back up the content.

For example, I want one text file in the "child" XAML file to reflect what is in the TextBox in the "parent" XAML file. I believe the following binding will work, but of course I cannot put that in the "child" XAML because it no longer "knows" about the parent test.

<TextBox Text="{Binding ElementName=test, Path=Text}" />


I'm pretty sure I have broken the "parent" XAML file incorrectly. I can't imagine that every time someone wants to break some XAML segment in another file, they have to rewrite their entire code and set up special linking hacks to keep the elements linked.

I've looked at a lot of posts (e.g. Binding two UserControls to the same DataContext or ViewModel? And What's the easiest way to break up large XAML files in my application? ), But they did not address my specific question.




source to share

1 answer

When you split your items into a UserControl, you can still access them by the x: Name field value you provided. However, since you are new to WPF, I would start learning the MVVM pattern before you start developing bad habits. It addresses your concerns specifically.



All Articles