在不同的窗口上更新列表视图 [英] Updating listviews on different windows

查看:76
本文介绍了在不同的窗口上更新列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

困境如下:

有一个基类,然后有两个窗口.第一个窗口是所有者.该窗口是被动的,包含一个列表视图,并链接到下一个窗口,而下一个窗口包含其他控件以及类似的列表视图.这两个窗口都绑定到相同的可观察集合,但是第二个窗口是唯一从每个操作的可观察集合中更新的窗口.除非重新加载或重新启动,否则第一个不会更新.

如何在第一个窗口与第二个窗口同时获取更新?

该窗口具有datacontext属性,如下所示

Hi All,

The dilemma is as follows:

There is a base class and then there are two windows. The first window is the owner. This window is passive containing a listview and is linked to the next window while the next window holds other controls and also a similar listview as well. Both windows are bound to the same observable collection but the second window is the only one that gets updated from the observable collection per operation. The first does not update unless reloaded or restarted.

How can I get the updates on the first window at the same time as that on the second?

The window has a datacontext property as follows

DataContext="{Binding RelativeSource={RelativeSource self}}"                    



listview的代码如下



The code for the listview is as follows

<ListView Name="interface_ListView" ItemsSource="{Binding interfaceList}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="64" Header="Port"

                                DisplayMemberBinding="{Binding portValue}" />
                            <GridViewColumn Width="150" Header="Type"

                                DisplayMemberBinding="{Binding portType}" />
                            <GridViewColumn Width="180" Header="Description"

                                DisplayMemberBinding="{Binding deviceDescription}" />
                            <GridViewColumn Width="64" Header="Vlan"

                                DisplayMemberBinding="{Binding vlanNumber}" />
                        </GridView>
                    </ListView.View>
                </ListView>




谢谢




Thanks

推荐答案

1>使用ObservableCollection<>创建一个通用视图模型类.列表属性.
1> Create a Common view model class with ObservableCollection<> list property.
public class CommonViewModel : INotifyPropertyChanged
{
    private ObservableCollection<string> items;
    public ObservableCollection<string> Items
    {
        get
        {
            return items;
        }
        set
        {
            items = value;
            OnPropertychanged("Items");
        }
    }

    private void OnPropertychanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}



2>将上述视图模型类的相同实例作为数据上下文传递给两个窗口.
例如.



2> Pass the same instance of above view model class to both windows as data context.
E.g.

public partial class Window1 : Window
  {
      public Window1()
      {
          InitializeComponent();
          CommonViewModel vm = new CommonViewModel();
          vm.Items = new ObservableCollection<string>(){"First","Second"};
          this.DataContext = vm;
          Window2 w2 = new Window2();
          w2.DataContext = vm;
          vm.Items.Add("Three");
          w2.Show();
      }
  }


<Window x:Class="DummyWPF.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="300" Width="300">
    <Grid>
        <ListView ItemsSource="{Binding Items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding}"></Label>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>







<Window x:Class="DummyWPF.Window2"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window2" Height="300" Width="300">
    <Grid>
        <ListView ItemsSource="{Binding Items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding}"></Label>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>




3>然后,您可以在任何位置进行修改,因为两个窗口都通过通用视图模型绑定到相同的属性,所以这两个窗口都将得到更新.




3> You then can modify anywhere you want, both windows will get update as both are bound to same property throught common view model.


这是基于窗口实现的适当接口的更通用的方法课.

创建一些接口,该接口应接受一些用于ListView更新的数据.如果此界面是关于纯数据的,并且不知道任何UI控件,那将是最好的.使两个Windows类都可以看到此界面.使用包含有问题的ListView的Window实现该接口,使用通过接口传递的数据更新列表视图.将接口引用传递给另一个窗口(使接口类型成为属性),但不传递给Window本身的实例的引用.这样,您就不会使任何访问说明符都更加开放,而不是真正需要它.

在接口的实现中,单独文件中的partial Windows声明很有帮助.请注意,在不同的部分声明中,您无需列出所有基类和接口,因此您可以创建一个单独的文件,在继承列表中使用该接口编写部分声明,而无需执行其他操作.您不必修改定义了相同类的其他文件.

—SA
This is a more general approach based on appropriate interface implemented by a window class.

Create some interface which should accept some data to use for the ListView update. It would be the best if this interface is about pure data, unaware of any UI controls. Make this interface visible by both of your Windows classes. Implemented this interface with the Window containing the ListView in question using data passed through interface to update the list view. Pass the interface reference to another window (make a property of interface type), but not the reference to the instance of the Window itself. This way, you do not make any access specifiers more open then it''s really needed.

In the implementation of interface, the partial windows declaration in a separate file is a great help. Note that in different partial declarations you don''t need to list all the base classes and interface, so you can create a separate file, write a partial declaration with the interface in inheritance list, nothing else. You don''t have to modify other files where the same class is defined.

—SA


我感谢两位贡献者所做的努力,以及我目前看来在上述解决方案的范围之内.我来详细说明...

我的基类类似于:
I thank both contributors for the effort and what I currently have seems to be within the lines of the above mentioned solutions. Let me elaborate...

My base class is similar to:
public class BaseClass : Window
{        
        public ObservableCollection<Item> _itemList = new ObservableCollection<Item>();
               
        public ObservableCollection<Item> itemList
        { get { return _vlanList; } }
}


并列出了与以下内容同义的类(在本例中为"Item"):


and have a class (in this case "Item") listed synonymous to:

public class Item
{
        public string type { get; set; }
        public string name { get; set; }
}


然后Window1将显示一个列表视图,其中包含通过Window2添加到列表视图中的项的详细信息.

Window1.cs就是这样:


then Window1 displays a listview that holds details of items that are added to the listview through Window2.

Window1.cs is as such:

public partial class Window1 : BaseClass
{
        public Window1()
        {
            //Deserializer(filePath);
            InitializeComponent();           
        }
}


Window1.xaml.cs->


Window1.xaml.cs ->

<ListView Name="interface_ListView" ItemsSource="{Binding itemList}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="64" Header="Name"

                                DisplayMemberBinding="{Binding name}" />
                            <GridViewColumn Width="150" Header="Type"

                                DisplayMemberBinding="{Binding type}" />
                        </GridView>
                    </ListView.View>
                </ListView>


Window2在继承和列表视图中与Window1同义,但是此处执行的操作如下:


Window2 is synonymous to Window1 in its inheritance and listview, but the operations are performed here such as:

_itemList.Add(new Item
            {
                name = text_Name.Text,
                type = text_Type.Text
            });


通过上面的描述,由于从该窗口执行了操作,因此更新了Window2,但是Window1保持休眠状态,直到刷新/重新加载为止(由于可以从xml数据文件中进行序列化和反序列化,因此可以检索到该数据).这两个窗口都从基本窗口引用可观察的集合,并将relativesource的数据上下文设置为"self"(也许问题就在那).

谢谢


With the above description, Window2 is updated since the operations are carried out from that window, however Window1 stays dormant until refreshed/reloaded (which is possible for the data to be retrieved since it is serialized and deserialized from an xml datafile). Both windows reference the observable collection from the base window and have the datacontext of relativesource set to "self" (perhaps the issue lies there).

Thanks


这篇关于在不同的窗口上更新列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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