Binding UserControl controls on Window to ViewModel

May 6, 2009 at 1:57 PM

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:





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






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


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














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


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






    Title="{Binding Path=VRATitle}"






<DockPanel Name="RootDockPanel" >



    <DockPanel DockPanel.Dock="Top" >



        <DockPanel.Children >



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









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?