Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,
I am trying to link two user control in WPF using one View Model class, The scenario is in UC1 i am having a text box and in UC2 also has text box if i change the text in UC1 text box it should reflect on UC2 textbox and vice verse. Below is my code.

VM Code:

C#
public class CommonViewModel : NotifyChangeClass
    {
        private string _Localtextdata;
        public string CommonProperity { get { return _Localtextdata; } set { _Localtextdata = value;
            NotifyPropertyChange("CommonProperity"); } }

    }

    public class NotifyChangeClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChange(string ProperityName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(ProperityName));
            }
        }
    }


UCI XAML:
XML
<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:Local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Local:CommonViewModel x:Key="ABC1"/>
    </UserControl.Resources>
    <StackPanel Margin="20,20,0,0" Height="100" DataContext="{Binding Source={StaticResource ABC1}}">
        <TextBox Text="{Binding CommonProperity ,Mode=TwoWay}" />
    </StackPanel>
</UserControl>


UC2 XAML:
XML
<UserControl x:Class="WpfApplication1.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:Local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Local:CommonViewModel x:Key="ABC"/>
    </UserControl.Resources>
    <StackPanel Margin="20,20,0,0" Height="100" DataContext="{Binding Source={StaticResource ABC}}">
        <TextBox Text="{Binding CommonProperity ,Mode=TwoWay}" />
    </StackPanel>
</UserControl>


Main Window XAML:

XML
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:Local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
<StackPanel>
     
        <Local:UserControl2></Local:UserControl2>
        <Local:UserControl1></Local:UserControl1>
    </StackPanel>

</Window>



Advance thanks

Arun
Posted

1 solution

You are creating different object of CommonViewModel in each user control, you have to create one object and assign them in both user control.

Create object of CommonViewModel in MainWindow

C#
<window.resources>
   <local:commonviewmodel x:key="ABC" />
</window.resources>


and then set data context for both user control UC1 and UC2 in your Main Window.

C#
<local:usercontrol1 datacontext="{Binding Source={StaticResource ABC}}" />
<local:usercontrol2 datacontext="{Binding Source={StaticResource ABC}}" />


Thanks
Shrey Chouhan
 
Share this answer
 
v2
Comments
arunrv 1-Aug-14 6:35am    
Hi Shrey thanks for your reply.
I is working but struck up in small problem .When ever the first text box type and mouse leaves then the second text box is reflected its not going smooth character by character when i goes on typing. When ever I leave the text box other text box gets reflected .
Shrey Chouhan 1-Aug-14 6:47am    
Use UpdateSourceTrigger property in textbox.

http://www.codeproject.com/Articles/507883/UpdateSourceTrigger-Property-in-WPF-Binding

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger(v=vs.110).aspx

arunrv 4-Aug-14 12:57pm    
Thanks Shrey :)it works.
Kapil Waghe 22-Jul-16 11:15am    
Hello..

Thanks Arun for asking this question and specially Shrey for the answer.

I'm working on same concept to share the ViewModel between more than one Views. Here Views are User Controls.

I have done this but having a small issue. Now, I want to switch between views under the DataTemplate.

1) HomeViewModel - Will have default view to show at first run.
2) AmericanFootballViewModel - This can have n number of views. ( THIS IS THE MAIN ISSUE FOR ME. ACTUALLY THIS IS MY FIRST PROJECT IN MVVM AND WPF :) )

Your help would be highly appreciable. I'm running into tight deadline.

Below example is just for reference as this will not work. It says we can add only one in AmericanFootballViewModel.

window.resources>
datatemplate datatype="{x:Type local:HomeViewModel}">
local:homeview>
/local:homeview>

datatemplate datatype="{x:Type vm:AmericanFootballViewModel}">
v_af:af_scoreboard_no_video x:name="Sai1">
/v_af:af_scoreboard_no_video>



dockpanel>
contentcontrol name="CurrentPage" content="{Binding CurrentPageViewModel}">
/contentcontrol>
sbarnes 4-Nov-18 15:39pm    
Here in Nov. 2018. Simple. Works. I can't believe how this answer could evade me for days and how many complicated bending-over-backwards solutions I tried to pursue.

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