简体   繁体   English

如何将viewModel绑定到视图

[英]How to bind the viewModel to view

I have created WPF code in the main window ,now I want to use MVVM. 我已经在主窗口中创建了WPF代码,现在我想使用MVVM。 I have copied all the data from the xaml of the main window to user control and created new class on the view model folder with the code which is in the xaml.cs class 我已经将所有数据从主窗口的xaml复制到用户控件,并使用xaml.cs类中的代码在视图模型文件夹中创建了新类。

in the user control class I add 在用户控件类中,我添加

public UserControl()
        {
            InitializeComponent();
     this.DataContext = new ModelView();
        }

currently there is two issue 目前有两个问题

1.In the main window I refer to ListBox as showen below ,and now probably the user control doesn't know about it,how should I solve it? 1.在主窗口中,如下图所示,我指的是列表框,现在用户控件可能不知道它,该如何解决?

the error on the listBox is "cannot access non-static property item source in static context". listBox上的错误是“无法在静态上下文中访问非静态属性项源”。

here for example I have error on the: ListBox.ItemsSource = _UsersList; 例如,这里出现以下错误: ListBox.ItemsSource = _UsersList;

class ModelView
    {       
          public ObservableCollection<User> _UsersList = new ObservableCollection<User>();


        public ObservableCollection<User> UserList
        {
            get { return _UsersList; }
        }

        public void initUsers()
        {
            _UsersList.Add(new User {Name = "Mike"});
            _UsersList.Add(new User {Name = "Jhon"});


            ListBox.ItemsSource = _UsersList;
        }

2.in addition in the view model I copied some code from the main window like method DropText_PreviewDragEnter which is refereed in Previ,ewDragEnter below in the XAML and now have error ,how should I avoid that? 2.另外,在视图模型中,我从主窗口中复制了一些代码,如方法DropText_PreviewDragEnter(该方法在Xvi中的Previ,ewDragEnter中引用),现在出现错误,应如何避免?

    <TextBox x:Name="FullName"  
              AcceptsReturn="True"
              AllowDrop="True" 
              PreviewDragEnter="DropText_PreviewDragEnter"


              HorizontalAlignment="Left" Height="20" Margin="360,70,0,0" TextWrapping="Wrap" Text="" 
              VerticalAlignment="Top" Width="70"/>

Regarding #1, your are refering the class ListBox, and not an instance of that class. 关于#1,您引用的是ListBox类,而不是该类的实例。 It is similar to doing somthing like: 这类似于做类似的事情:

string = "a". 字符串=“ a”。

What you should do is write a ListBox in your xaml, in which you will bind it's ItemSource property into your UserList property in the view model. 您应该做的是在xaml中编写一个ListBox,在其中将其ItemSource属性绑定到视图模型中的UserList属性。

I suggest you find an example on the internet for binding a listbox to see the concept. 我建议您在Internet上找到一个示例,以绑定列表框以查看该概念。

Regarding #2, it is not clear what is the error you are reciving, but make sure you have implemented a DropText_PreviewDragEnter method in the code behind of the xaml. 关于#2,尚不清楚您正在接收什么错误,但是请确保已在xaml后面的代码中实现了DropText_PreviewDragEnter方法。

First off, you are setting the ItemsSource in the wrong place - you need to set the ItemsSource in the xaml page of your UserControl. 首先,您将ItemsSource设置在错误的位置-您需要在UserControl的xaml页面中设置ItemsSource。

<ListBox ItemsSource="{Binding _UsersList}"

As for the second question - you haven't given us enough to help you with. 至于第二个问题-您给我们的帮助不足。 Do you have a code behind event associated with that event? 您是否有与该事件关联的事件背后的代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM