Click here to Skip to main content
14,391,749 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a Main Window created in C#, WPF. This main window makes use of tabs, each tab corresponding to an application which is actually a User Control. The Main Window provides common information to each of those User Controls, i.e. user name, state of database, a transaction id.

We have all this integrated just fine, up and running. However, I am having trouble, a lot of trouble passing data to the User Controls using MVVM. If I just use "code behind", then it's fine, the Main Window can pass data to the User Control.

Quick overview:
Main Window is up and running - uses MVVM.

User Control referenced via the Main Window's XAML can reference Main Window data using Dependence Properties in the code behind.
I am unable to reference that same Main Window data in the User Control's View Model. Can anyone help?
Rate this:
Please Sign up or sign in to vote.

Solution 1

This "issue" is not on the MVVM level. You simply need to add a member to your user control designed to accept data from the UI elements using control: a property (could optionally be a write-only property, without a getter) or a "Set" method with some input data parameters.

I cannot imagine how it could be a problem. But, anyway, if you face some, please ask some follow-up questions.

Ganesh KP 15-Jul-14 12:30pm
I think @Sergey gave you the answer for you. Create a property of your object that have all the properties that you need to bind to the control and call from main window and set, it. Its easy job.
Sergey Alexandrovich Kryukov 15-Jul-14 12:57pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

I have just started C# programming could you provide more details...example?

On my end, here is a little more detail:

Main Window XAML:
<UC:DeliveryControl UC1="{Binding Path=ContainerData, Mode=TwoWay}" />

User Control XAML (i.e. DeliveryControl):
<TextBox x:Name="tbxConDat" HorizontalAlignment="Left" Height="23" Margin="641,443,0,0" TextWrapping="Wrap" Text="{Binding UC1, ElementName=root}" VerticalAlignment="Top" Width="120" Focusable="False" IsEnabled="False" TextChanged="tbxConDat_TextChanged" RenderTransformOrigin="0.408,19.783" Visibility="Hidden"/>

User Control Code Behind:
<pre lang="cs">public static readonly DependencyProperty UC1Property = DependencyProperty.Register(&quot;UC1&quot;, typeof(string),

typeof(DeliveryControl), new PropertyMetadata((string)&quot;&quot;));

public string UC1


get { return (string)GetValue(UC1Property); }



SetValue(UC1Property, value);



public static readonly DependencyProperty UCESIProperty = DependencyProperty.Register(&quot;UCESI&quot;, typeof(string),

typeof(DeliveryControl), new PropertyMetadata((string)&quot;&quot;));</pre>

This all works fine except that I cannot access UC1 from the User Control (DeliveryControl) View Model.
Member 10558493 15-Jul-14 19:24pm
Sorry guys, this isn't the solution, but I wasn't able to put all the code snippets in as a "comment"...
Use "Improve question" instead...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100