Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (1 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?
Posted

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.

—SA
 
Share this answer
 
v2
Comments
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    
Certainly.
—SA
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); }

set

{

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.
 
Share this answer
 
Comments
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"...
Sergey Alexandrovich Kryukov 18-Jul-14 2:32am    
Use "Improve question" instead...
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900