Passing Data from Main Window to a User Control's VewModel

3 posts, 0 answers
  1. Stephen
    Stephen avatar
    17 posts
    Member since:
    Aug 2013

    Posted 31 Jul 2014 Link to this post

    I am coding in C#, using the MVVM pattern. I have a Main Window that uses several User Controls. I am trying to pass string data fro the Main Window to the View Model of the User Control. So far I have been able to pass the Main Window data to the User Control View and View code behind. However, I can't seem to expose that data to the User Control View Model. Here is what does work:

    Main Window XAML,

    UCV:SetupUC UC1="{Binding DataContext.ContainerData, ElementName=winCon, Mode=TwoWay}">

    User Control XAML,

    <TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding UC1, ElementName=root, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.017,0.304"/>

    User Control XAML code behind,

    public partial class SetupUC : UserControl, INotifyPropertyChanged

            public static readonly DependencyProperty UC1Property =
          "UC1", typeof(string), typeof(SetupUC),
            new FrameworkPropertyMetadata()
                PropertyChangedCallback = OnUC1Changed,
                BindsTwoWayByDefault = true
            public string UC1
                get { return (string)GetValue(UC1Property); }
                    SetValue(UC1Property, value);
            private static void OnUC1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
                if (e.OldValue != e.NewValue)
                    // code to be executed on value update
            public SetupUC()
                SetupViewModel svm = new SetupViewModel();
                this.DataContext = svm;
                Binding binding = new Binding("ViewModelStringProperty") { Source = svm, Mode = BindingMode.TwoWay };
                BindingOperations.SetBinding(this, SetupUC.UC1Property, binding);

    The last 2 lines above are an attempt to bind the DP to a property in the User Control View Model - which other posts have suggested as a possible solution. I have not been able to get the binding to work. Again, the TextBox x:Name="tbx1" is displaying the DP, UC1, just fine; it changes "realtime" when the bound data in the Main Window changes.
  2. Yana
    Yana avatar
    4548 posts

    Posted 01 Aug 2014 Link to this post

    Hello Stephen,

    This is quite a general question, not related to any of the controls from UI for WPF suite, that's why I would suggest that you also post it on MSDN forums or StackOverflow.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
  3. DevCraft banner
  4. Stephen
    Stephen avatar
    17 posts
    Member since:
    Aug 2013

    Posted 01 Aug 2014 in reply to Yana Link to this post

    Thanks, I did try fact there is quite a lengthy post in stackoverflow trying to figure it out...just thought this community might want a shot at it. For what it's worth, we are using Telerik controls and this situation is using Telerik controls, but, yes, it is not unique to Telerik controls.
Back to Top