Binding UserControl controls on Window to ViewModel

May 6, 2009 at 1:57 PM
Hi,

We have been struggling with trying to figure out how to bind data that is in the ViewModel to controls that are in a UserControl on the Window. There is probably some really simple way to do this but I haven't figured it out. I have many examples of how I need to use this but here is one in particular. I have a ribbon control that I have put in a UserControl. I want to bind the Title of the ribbon control to the dependency property VRATitle that I have in the ViewModel. Here is what I have in the UserControl:

 

<Grid>

 

 

<r:Ribbon x:Name="ribbon" Title="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=VRATitle}" FlowDirection="LeftToRight" >

 

 

<r:Ribbon.Resources>

 


...

This gets me to the parent of the UserControl. The main window xaml has the following

<

r:RibbonWindow x:Class="vRA.WPF.UI.PenskeRouteAssist"

 

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

 

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

 

    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"

 

    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"

 

    xmlns:local="clr-namespace:vRA.WPF.UI"

 

    xmlns:uc="clr-namespace:vRA.WPF.UI.Controls"

 

    Height="686" Width="799" WindowStartupLocation="CenterScreen"

 

    FontFamily="Arial" Background="{DynamicResource WindowBackground}"

 

    xmlns

:custom="clr-namespace:DockingLibrary;assembly=DockingLibrary"

    Closing="OnClosing"

 

    Title="{Binding Path=VRATitle}"

 

 

>

 

 

<DockPanel Name="RootDockPanel" >

 

 

    <DockPanel DockPanel.Dock="Top" >

 

 

        <DockPanel.Children >

 

 

            <uc:vRARibbonControl x:Name="vRARibbonControl" />

 

 

        </DockPanel.Children>

 

 

</DockPanel>

 

...

Since the DataContext for the main window is the ViewModel this does seem to work - but I'll bet it's anything but the right way to do it.


In other cases I have many controls in the UserControl that I place on a page. How would I get the "City" data from the window's ViewModel for binding. Please excuse me if I am way off on my attempted solution here as I am really new to WPF/XAML and the whole M-V-VM design. Can anyone shed any light on how to connect this up?

thanks!
Bill