anonmous
anonmous

Reputation: 139

How do I break a large XAML file into sub-XAML files and maintain communication between the parent and child objects?

I'm new to WPF. I'm attempting to modify the project VisualStudioLikePanes from the book WPF 4 Unleashed. Because the panes are hidden by default until I run the project, I decided that it would be nice to place the pane I'm working on into a separate xaml file so that I can see the changes I make to the pane without needing 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 plopped the content of the pane in question into that. Here is what the UserControl attributes look like in the 'child' XAML file:

<UserControl x:Class="Sample.SettingsPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">

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

<Window
    Title="MainWindow"
    x:Uid="Window_1" x:Class="Sample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sp="clr-namespace:Sample"
    x:Name="window">

I then 'included' the control via this:

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

I immediately found that in the code-behind file for the 'parent' XAML file, all of the code which made reference to any of the elements now contained in the 'child' XAML file were now unrecognized. So, I then removed (or commented out) all references to names and objects which were now contained within the 'child' XAML file and ever since then have been jumping through hoops to wire things back up.

For example, I want one TextBox in the 'child' XAML file to reflect what is in a TextBox in the 'parent' XAML file. I believe that the following binding would work, but, of course, I can't place this into the 'child' XAML, because it doesn't 'know' about the parent's 'test' element any longer.

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

I'm sure I've broken up the 'parent' XAML file incorrectly. I can't imagine that everytime somebody wants to break some segment of XAML out to another file they must rework all of their code behind and set up special communication hacks to let elements continue to communicate.

I did look at a variety of posts (e.g. Binding two UserControls to the same DataContext, or ViewModel? and What is the easiest way to break up large XAML files in my application?), but they didn't address my particular question.

Thanks,

Matt

Upvotes: 4

Views: 939

Answers (1)

erodewald
erodewald

Reputation: 1825

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

Upvotes: 1

Related Questions