Data Binding multiple TextBox’s which live in different controls

Jul 25 2008 10:38 PM

I have an Expander Group Control, which is basically just an Expander with a border around it.

 

Here’s the xaml for the ExpanderGroupControl

 

<UserControl x:Class="WpfBinding.ExpanderGroupControl"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Expander

        BorderThickness="1"

        Background="LightGray"

        VerticalAlignment="Top"

        Name="Expander"

        IsExpanded="True"

        Collapsed="ExpanderGroupControl_Collapsed" 

        Expanded="ExpanderGroupControl_Expanded"

        SnapsToDevicePixels="True" >

        <Border Margin="3"  Background="White" Padding="5">

            <StackPanel Name="expanderPanel" >

            </StackPanel>

        </Border>

    </Expander>

</UserControl>

 

Here’s the .cs for the ExpanderGroupControl

 

using System.Windows;

 

namespace WpfBinding

{

    public partial class ExpanderGroupControl

    {

 

        public ExpanderGroupControl()

        {

            InitializeComponent();

 

            Expander.Height = double.NaN;

        }

 

        private void ExpanderGroupControl_Expanded( object sender, RoutedEventArgs e )

        {

            Expander.Height = double.NaN;

        }

 

        private void ExpanderGroupControl_Collapsed( object sender, RoutedEventArgs e )

        {

            Expander.Height = 25;

        }

    }

}

 

From a list of policies (xaml read from disk), I need to display the content into a different ExpanderGroupControl.  Here’s the code for this.

 

...

    elementHost1.Controls.Clear();

 

    ScrollViewer scrollViewer = new ScrollViewer();

 

    scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

    scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;

 

    StackPanel stackPanel = new StackPanel();

    scrollViewer.Content = stackPanel;

 

    elementHost1.Child = scrollViewer;

 

    foreach ( PolicyItem item in PolicyList )

    {

        String xaml = "";

 

        if ( item != null )

        {

            xaml = item.XamlData;

        }

 

        ExpanderGroupControl expander = new ExpanderGroupControl();

        expander.Expander.Header = item.Name;

 

        UIElement xamlElement = (UIElement)XamlReader.Load( new XmlTextReader( new StringReader( xaml ) ) );

        expander.expanderPanel.Children.Add( xamlElement );

        stackPanel.Children.Add( expander );

    }

...

 

Here’s the situation:  Each xaml entry can have a custom TextBox that needs to be data bound to other TextBox’s in a different xaml object.  So for example, I have 3 policies A,B,C - each with a TextBox defined in each.  If the user changes TextBox from A it needs to update the TextBox's for B & C.  If B changes then A & C get updated, and the same if C changes.

I have tried several different ways to bind this data (none displayed above), but nothing seems to work. Is it because the source isn’t defined in the same control (I’m newing up a new ExpanderGroupControl for each policy)?  What’s a valid solution to this problem?  Any help is greatly appreciated!


Thanks in advance

 


Brought to you by: