再一次,将数据从主窗口传递到用户控制到UC视图模型...帮助? [英] One more time, passing data from Main window to to User Control to UC View Model...help?

查看:87
本文介绍了再一次,将数据从主窗口传递到用户控制到UC视图模型...帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个包含多个用户控件的C#WPF应用程序。其中一个用户控件需要来自此MainWindow的数据才能正确显示其他信息并自行处理。 (现在,只需在MainWindow和用户控件之间传递一个字符串即可。)我已经可以通过依赖属性(在后面的UC代码中)将数据绑定到用户控件XAML,并显示精细。但是,我无法访问相同的数据(显示在与UC相关联的视图模型中。



以下是相关代码段:



MainWindow XAML:



Currently, I have a working C# WPF application consisting of several User Controls. One of the User Controls needs data from this MainWindow to properly display additional information and process itself. (For now, it would be satisfactory just to pass a string between the MainWindow and the User Control.) I already am able via a Dependency Property (in the UC code behind) to bind the data to the User Control XAML, and it displays fine. However, I am unable to access that same data (that is being displayed in the view model that is tied to the UC.

Here are related code snippets:

MainWindow XAML:

<UCV:SetupUC UC1="{Binding DataContext.ContainerData, ElementName=winCon, Mode=TwoWay}">
</UCV:SetupUC>





SetupUC XAML(这是用户控件) :



< TextBox x:Name =tbx1Horizo​​ntalAlignment =LeftHeight =23Margin =159,22,0,0TextWrapping = WrapText ={Binding UC1,ElementName = root}VerticalAlignment =TopWidth =120/&g t;



注意,这显示正常,包括更改MainWindow中的值更改。



后面的SetupUC代码:





SetupUC XAML (this is the User Control):

<TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding UC1, ElementName=root}" VerticalAlignment="Top" Width="120"/>

Note, this is displaying fine, including changing as the value changes in the MainWindow.

SetupUC Code behind:

public partial class SetupUC : UserControl, INotifyPropertyChanged
    {
public static readonly DependencyProperty UC1Property = DependencyProperty.Register("UC1", typeof(string),
            typeof(SetupUC), new PropertyMetadata(OnUC1Changed));
        public string UC1
        {
            get { return (string)GetValue(UC1Property); }
            set
            {
                SetValue(UC1Property, value);
            }
        }

public SetupUC()
        {
            InitializeComponent();
            SetupViewModel svm = new SetupViewModel(UC1);
            this.DataContext = svm;
        }
}







最后,SetupViewModel:






Finally, SetupViewModel:

public class SetupViewModel : INotifyPropertyChanged
   {
       public SetupViewModel(string UC2)
       {
           ContainerData = UC2;
           _UserProfile = new UserProfile();
           UserProfile.Pwd = ContainerData;
       }
public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string _containerData;
        public string ContainerData
        {
            get { return _containerData; }
            set
            {
                _containerData = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ContainerData"));
                }
            }
        }
    }





UC1在后面的代码中在SetupUC视图中显示,似乎永远不会传递给视图模型UC2。有趣的是,如果我用类似123的字符串替换后面的代码中的UC1,它将被传递到UC2中的VM。



我很新C#和MVVM,我一直在谷歌搜索这几天。发现了很多帖子并且有不同的recs。从这些帖子中,但仍然没有运气......



请帮忙吗?!?



UC1 in the code behind which does display in the SetupUC view, never seems to pass to the view model, UC2. Interestingly enough, if I replace the UC1 in the code behind with a string like "123" it will get passed to the VM in UC2.

I am pretty new to C# and MVVM, and I have been googling this for days. Found a lot of posts and have trid different recs. from those posts, but still no luck...

Help, please?!?

推荐答案

Should the binding in the user control for the text box be ContainerData instead of UC1?

<TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding ContainerData, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>


这篇关于再一次,将数据从主窗口传递到用户控制到UC视图模型...帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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