将Common View模型对象共享到WPF中的用户控件 [英] Sharing Common View model object to user controls in WPF

查看:95
本文介绍了将Common View模型对象共享到WPF中的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我试图使用一个View Model类在WPF中链接两个用户控件,场景是在UC1中我有一个文本框,在UC2中也有文本框如果我更改UC1文本框中的文本应该反映在UC2文本框中,反之亦然。以下是我的代码。



VM代码:



  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;

受保护 void NotifyPropertyChange( string ProperityName)
{
if (PropertyChanged!= null
{
PropertyChanged( this new PropertyChangedEventArgs(ProperityName));
}
}
}





UCI XAML:

< pre lang =xml> < UserControl x:Class = WpfApplication1.UserControl1

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

< span class =code-attribute> xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

xmlns:mc = http:// schemas .openxmlformats.org / markup-compatibility / 2006

< span class =code-attribute> xmlns:d = http://schemas.microsoft.com/expression/blend/2008

< span class =code-attribute> xmlns:Local = clr-namespace:WpfApplication1

mc:可忽略 = d

d:DesignHeight = 300 d:DesignWidth = 300 >
< UserControl.Resources >
< Local:CommonViewModel x:Key = ABC1 / >
< < span class =code-leadattribute> / UserControl.Resources >
< StackPanel 保证金 = 20,20,0,0 < span class =code-attribute>高度 = 100 DataContext = {Binding Source = {StaticResource ABC1}} >
< TextBox 文字 = {Binding CommonProperity,Mode = TwoWay} / >
< / StackPanel >
< / UserControl >





UC2 XAML:

 <   UserControl     x:Class  < span class =code-keyword> =  WpfApplication1.UserControl2  

< span class =code-attribute> xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

< span class =code-attribute> xmlns:x = http: //schemas.microsoft.com/winfx/2006/xaml\"

< span class =code-attribute> xmlns:mc = http://schemas.openxmlformats。 org / markup-compatibility / 2006

xmlns:d = http://schemas.microsoft.com/expression/blend / 2008

< span class =code-attribute> xmlns :本地 = clr-namespace:WpfApplication1

mc:Ignorable = d

< span class =code-attribute> d:DesignHeight = 300 d:DesignWidth = 300 >
< UserControl.Resources >
< Local:CommonViewModel x:密钥 = ABC / >
< / UserControl.Resources >
< StackPanel 保证金 = 20,20,0,0 高度 = < span class =code-keyword> 100
DataContext = {Binding Source = {StaticResource ABC}} >
< TextBox < span class =code-attribute> Text = {Binding CommonProperity,Mode = TwoWay} / >
< / StackPanel >
< / UserControl >





主窗口XAML:



 <   Window     x:Class   =  WpfApplication1.MainWindow  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

< span class =code-attribute>
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

< span class =code-attribute> xmlns:Local = clr-namespace:WpfApplication1

标题 = MainWindow 高度 = 350 宽度 = 525 >
< < span class =code-leadattribute> StackPanel
>

< 本地:UserControl2 > < / Local:UserControl2 >
< Local:UserControl1 > < / Local:UserControl1 >
< / StackPanel >

< /窗口 >







预先感谢



Arun

解决方案

您正在每个用户控件中创建CommonViewModel的不同对象,你必须创建一个对象并在两个用户控件中分配它们。



在MainWindow中创建CommonViewModel的对象



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





然后在主窗口中为用户控件UC1和UC2设置数据上下文。



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





谢谢

Shrey Chouhan


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:

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:

<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:

<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:

<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

解决方案

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

<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.

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



Thanks
Shrey Chouhan


这篇关于将Common View模型对象共享到WPF中的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆