This is a migrated thread and some comments may be shown as answers.

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

2 Answers 1070 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Iron
Stephen asked on 31 Jul 2014, 09:27 PM
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}">
</UCV:SetupUC>

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 =
    DependencyProperty.Register(
      "UC1", typeof(string), typeof(SetupUC),
        new FrameworkPropertyMetadata()
        {
            PropertyChangedCallback = OnUC1Changed,
            BindsTwoWayByDefault = true
        });
        public string UC1
        {
            get { return (string)GetValue(UC1Property); }
            set
            {
                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()
        {
            InitializeComponent();
            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 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 01 Aug 2014, 09:49 AM
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.

Regards,
Yana
Telerik
 
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.
 
0
Stephen
Top achievements
Rank 1
Iron
answered on 01 Aug 2014, 03:05 PM
Thanks, I did try that...in 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.
:-)
Tags
General Discussions
Asked by
Stephen
Top achievements
Rank 1
Iron
Answers by
Yana
Telerik team
Stephen
Top achievements
Rank 1
Iron
Share this question
or