Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体到WPF MVVM_C#_.net_Wpf_Xaml_Mvvm - Fatal编程技术网

C# Windows窗体到WPF MVVM

C# Windows窗体到WPF MVVM,c#,.net,wpf,xaml,mvvm,C#,.net,Wpf,Xaml,Mvvm,我正在使用MVVM模式和绑定进行从Windows窗体到WPF的代码重构 我的Windows窗体代码: searchCriteriaListBox.Items.Add("Cat"); 我的WPF XAML: <ListBox Height="288" HorizontalAlignment="Left" Margin="18,206,0,0" VerticalAlignment="Top" Width="1042" /> 如何使用WPF绑定实现相同的“添加”操作?将页面/窗口/

我正在使用MVVM模式和绑定进行从Windows窗体到WPF的代码重构

我的Windows窗体代码:

searchCriteriaListBox.Items.Add("Cat");
我的WPF XAML:

<ListBox Height="288" HorizontalAlignment="Left" Margin="18,206,0,0" VerticalAlignment="Top" Width="1042" />


如何使用WPF绑定实现相同的“添加”操作?

将页面/窗口/用户控件的DataContext(根级别的内容)设置为ViewModel

在ViewModel中创建属性

使用以下内容作为searchCriteriaListBox的绑定

 {Binding Items, Mode=TwoWay}

<ListBox  ItemsSource="{Binding Items, Mode=TwoWay}" Height="288" HorizontalAlignment="Left" Margin="18,206,0,0" VerticalAlignment="Top" Width="1042" />
{绑定项,模式=双向}
在视图模型中,放置AddItemCommand

在AddItemCommand.Execute中,将项添加到项集合


用于可重用项的位,并将页面/窗口/用户控件的数据上下文(根级别的内容)读取到ViewModel

在ViewModel中创建属性

使用以下内容作为searchCriteriaListBox的绑定

 {Binding Items, Mode=TwoWay}

<ListBox  ItemsSource="{Binding Items, Mode=TwoWay}" Height="288" HorizontalAlignment="Left" Margin="18,206,0,0" VerticalAlignment="Top" Width="1042" />
{绑定项,模式=双向}
在视图模型中,放置AddItemCommand

在AddItemCommand.Execute中,将项添加到项集合


用于可重用项的位,并读取

将您的列表框绑定到可观察的集合,然后只需在此集合中添加/删除要更新的项,您不应该在xaml中向该框添加数据


看一看这个视频,我发现它在学习mvvm时非常有用-

将列表框绑定到一个可观察的集合,然后简单地从这个集合中添加/删除要更新的项,您不应该在xaml中将数据添加到框中


看看这段视频,我发现它在学习mvvm时非常有用-

我认为一行代码不足以让我们告诉您如何最好地转换它…我认为一行代码不足以让我们告诉您如何最好地转换它。。。