如何将数据从MainWindow传递到MainWindow内部的用户控件? [英] How to pass data from MainWindow to a User Control that's inside the MainWindow?

查看:55
本文介绍了如何将数据从MainWindow传递到MainWindow内部的用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了研究,发现人们倾向于使用ViewModel来实现这一目标,但是我仍然陷于其中.

I did my research that people tend to use ViewModel to achieve this but I am sort of stuck in it.

我有一个

公共ObservableCollection< Order>orderList {get;放;} = new ObservableCollection< Order>();

MainWindow 中,它已经充满了数据.

in MainWindow which is already filled up with data.

在MainWindow XAML中,我在 TabControl 内有一个用户控件:

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>

我们在这里只谈论UC1,因此在UC1 XAML中,我里面有一个 ListView :

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>

这是UserControl1VM.cs中的代码:

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.

我的目标只是填充 ListView ,如果 orderList 更改,它将自动更新.但是现在我什至不知道ViewModel是否正常工作,任何想法和代码演示都将不胜感激.

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.

推荐答案

在将UserControl分配给UserControl的XAML中的DataContext时,UserControl永远不应具有私有"视图模型.相反,它应该公开可以绑定到外部提供的视图模型对象的属性的依赖项属性.

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.

像这样声明 ItemsSource 属性:

public partial class UserControl1 : UserControl
{
    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register(
            nameof(ItemsSource), typeof(IEnumerable), typeof(UserControl1));

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public UserControl1()
    {
        InitializeComponent();
    }
}

并像这样绑定ListView:

And bind the ListView like this:

<UserControl ...>
    ...
    <ListView ItemsSource="{Binding ItemsSource,
                            RelativeSource={RelativeSource AncestorType=UserControl}}">
        ...
    </ListView>
    ...
</UserControl>

使用UserControl时,将属性绑定到视图模型属性:

When you use the UserControl, bind the property to a view model property:

<TabItem Header="UC1">
    <local:UserControl1 ItemsSource="{Binding OrderList}"/>
</TabItem>

最后一个XAML代码段假定UserControl的DataContext中的对象具有 OrderList 属性.当TabControl绑定到具有该属性的视图模型对象的集合时,会自动发生这种情况.

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.

这篇关于如何将数据从MainWindow传递到MainWindow内部的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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