如何将数据从MainWindow传递到MainWindow内部的用户控制?
我进行了研究,人们倾向于使用ViewModel来实现这一目标,但我有点陷入困境。
我有一个
public observableCollection< order>订购列表{get;放; } =新的ObservableCollection< order>();
在mainWindow
中,该已经填充了数据。
在MainWindow XAML中,我在TabControl
内有一个用户控件:
<TabControl x:Name="TabCollection">
<TabItem Header="UC1">
<local:UserControl1/>
</TabItem>
<TabItem Header="UC2">
<local:UserControl2/>
</TabItem>
</TabControl>
我们仅在此处讨论UC1,因此在UC1 XAML中,我有一个listView
insire:
<UserControl.DataContext>
<local:UserControl1VM/>
</UserControl.DataContext>
<ListView x:Name="ListViewText">
<ListView.View>
<GridView>
<GridViewColumn Header="First name" DisplayMemberBinding="{Binding Firstname}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Lastname}"/>
<GridViewColumn Header="Order" DisplayMemberBinding="{Binding Ordername}"/>
<GridViewColumn Header="Delivery time" DisplayMemberBinding="{Binding Deliverytime}"/>
<GridViewColumn Header="Phone Number" DisplayMemberBinding="{Binding Phone}"/>
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}"/>
<GridViewColumn Header="Email" DisplayMemberBinding="{Binding Email}"/>
</GridView>
</ListView.View>
</ListView>
这是USERCORERCONTROL1VM中的代码。 CS:
namespace QuickShop
{
class UserControl1VM : INotifyPropertyChanged
{
private ObservableCollection<Order> orderList;
public ObservableCollection<Order> OrderList
{
get { return orderList; }
set
{
orderList = value;
PropertyChanged(this, new PropertyChangedEventArgs("OrderList"));
}
}
//
private void FindDeliveryOrders(IEnumerable<Order> sortList)
{
foreach (var order in sortList)
{
if (order.Delivery.Equals("Yes"))
{
//deliveryOrders.Add(order);
this.ListViewText.Items.Add(new Order { Firstname = order.Firstname, Lastname = order.Lastname, Ordername = order.Ordername, Deliverytime = order.Deliverytime, Phone = order.Phone, Address = order.Address, Email = order.Email });
}
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
}
当然,这些是不完整的代码,因为我不知道下一步如何进行。
我的目标只是填充listView
,如果orderList
更改,它将自动更新自身。但是现在,我什至不知道ViewModel是否有效,任何想法和代码演示都会非常感谢。
I did my research that people tend to use ViewModel to achieve this but I am sort of stuck in it.
I have a
public ObservableCollection<Order> orderList { get; set; } = new ObservableCollection<Order>();
in MainWindow
which is already filled up with data.
in MainWindow XAML I have a User Control inside the TabControl
:
<TabControl x:Name="TabCollection">
<TabItem Header="UC1">
<local:UserControl1/>
</TabItem>
<TabItem Header="UC2">
<local:UserControl2/>
</TabItem>
</TabControl>
We only talk about UC1 here so in UC1 XAML here I have a ListView
inside:
<UserControl.DataContext>
<local:UserControl1VM/>
</UserControl.DataContext>
<ListView x:Name="ListViewText">
<ListView.View>
<GridView>
<GridViewColumn Header="First name" DisplayMemberBinding="{Binding Firstname}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Lastname}"/>
<GridViewColumn Header="Order" DisplayMemberBinding="{Binding Ordername}"/>
<GridViewColumn Header="Delivery time" DisplayMemberBinding="{Binding Deliverytime}"/>
<GridViewColumn Header="Phone Number" DisplayMemberBinding="{Binding Phone}"/>
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}"/>
<GridViewColumn Header="Email" DisplayMemberBinding="{Binding Email}"/>
</GridView>
</ListView.View>
</ListView>
And here's the code in UserControl1VM.cs:
namespace QuickShop
{
class UserControl1VM : INotifyPropertyChanged
{
private ObservableCollection<Order> orderList;
public ObservableCollection<Order> OrderList
{
get { return orderList; }
set
{
orderList = value;
PropertyChanged(this, new PropertyChangedEventArgs("OrderList"));
}
}
//
private void FindDeliveryOrders(IEnumerable<Order> sortList)
{
foreach (var order in sortList)
{
if (order.Delivery.Equals("Yes"))
{
//deliveryOrders.Add(order);
this.ListViewText.Items.Add(new Order { Firstname = order.Firstname, Lastname = order.Lastname, Ordername = order.Ordername, Deliverytime = order.Deliverytime, Phone = order.Phone, Address = order.Address, Email = order.Email });
}
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
}
And Of course these are incomplete codes because I don't know how to proceed next.
My goal is just to populate the ListView
and it will automatically update itself if orderList
changes. But right now I couldn't even know whether the ViewModel is working or not, any thoughts and code demo would be very grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
USERCONTROL绝不能具有“私有”视图模型,因为您将其分配给USERCONTROL的XAML中的DataContext。取而代之的是,它应该公开可以绑定到外部提供的视图模型对象的属性的依赖性属性。
声明
itemssource
诸如此类的属性:并以这样的方式绑定listView:
当您使用usercontrol时,将属性绑定到视图模型属性:
最后一个XAML代码段假设usercontrol的dataContext中的对象具有
OrderList
属性。当TabControl绑定到具有该属性的视图模型对象集合时,这将自动发生。或者,,让USERCONTROL XAML中的元素直接与继承的DataContext中对象的属性结合。
您的控件不必公开其他可绑定的属性,但它仅适用于实际提供预期源属性的DataContext对象。
A UserControl should never have a "private" view model, as you assign it to the DataContext in the UserControl's XAML. It should instead expose dependency properties that could be bound to properties of an externally provided view model object.
Declare an
ItemsSource
property like this:And bind the ListView like this:
When you use the UserControl, bind the property to a view model property:
The last XAML snippet assumes that the object in the UserControl's DataContext has a
OrderList
property. This would automatically happen when the TabControl is bound to a collection of view model objects with that property.Alternatively, let the elements in the UserControl's XAML directly bind to the properties of the object in the inherited DataContext.
Your control would not have to expose additional bindable properties, but it would only work with DataContext objects that actually provide the expected source properties.