Datagrid可观察的集合的多个视图 [英] Multiple Views of Observable Collection with Datagrid

查看:121
本文介绍了Datagrid可观察的集合的多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有同样的问题,如。但是我使用的是DataGrid而不是ListBox,它似乎并没有像这样工作(这可能是因为我从来没有使用visual basic,并且没有将代码转换成c#)。
我基本上希望使用不同的过滤器在同一数据上使用两个DataGrids。

  ICollectionView view_dataLinesUnfiltered; 
ICollectionView view_dataLinesFiltered;

public MainWindow()
{
...
// view_dataLines = CollectionViewSource.GetDefaultView(dataLines); //< - 过滤器适用于
view_dataLinesUnfiltered = new CollectionView(dataLines); //< - 过滤器不起作用
view_dataLinesFiltered = new CollectionView(dataLines);
....
//控件事件
this.ShowAA.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ToggleButton.UncheckedEvent));

}

private void ShowAA_Checked(object sender,RoutedEventArgs e)
{
view_dataLinesUnfiltered.Filter = null;
}

private void ShowAA_UnChecked(object sender,RoutedEventArgs e)
{
view_dataLinesUnfiltered.Filter = delegate(object o){return FilterContent(o as ErrorDetection.stDataLine ,AA,); };
}

bool FilterContent(ErrorDetection.stDataLine line,string sFilterAA,string sFilter)
{
shortArrayToHexStringConverter converter = new shortArrayToHexStringConverter();

string comBuffer = convert.Convert(line.ComBufferP as object,typeof(string),0,System.Globalization.CultureInfo.CurrentCulture)as string;

return false; //!comBuffer.Contains(AA);
}

FilterContent方法被调用没有问题,但DataGrid反正显示了这些行。如果我使用GetDefaultView,Filter可以在Datagrids上使用。我必须使用其他视图而不是CollectionView(ListCollectionView也不起作用)?



我已经做了一个小的示例项目来显示问题示例。它只包含一个构造函数和一个可观察的集合。

解决方案

您需要指定哪个ICollectionVIew用于哪个DataGrid。



如果你刚刚绑定到集合(在这种情况下是dataLines),WPF将使用默认视图(如果需要,可以创建一个),这就是为什么第一个评论外线用于过滤。



有几种方法可以指定哪个视图用于哪个数据网格,具体取决于您正在使用的模式等。 >

1)像链接的问题一样,您可以在初始化视图后,在窗口的代码中为每个DataGrid设置ItemsSource,例如:

  filteredDataGrid.ItemsSource = view_dataLinesFiltered; 
unfilteredDataGrid.ItemsSource = view_dataLinesUnfiltered;

2)您可以将窗口的DataContext设置为自己,或为屏幕创建视图模型包含视图,并使视图为公共属性,然后绑定到每个网格的预期视图,例如

 < DataGrid ItemsSource ={Binding View_dataLinesFiltered}> .... 






编辑:



现在我不在工作,可以得到dropbox和玩你的例子似乎是奇怪的行为的原因是直接使用CollectionView。在收集视图的msdn页面中,它表示


您不应该在代码中创建此类的对象。要为仅实现IEnumerable的集合创建
集合视图,
创建一个CollectionViewSource对象,将您的集合添加到
Source属性,并从View属性获取集合视图。 p>

但是,如果您不想在XAML中设置视图,还可以将您的CollectionViews更改为 ListCollectionViews ,它应该按预期工作(这很可能是视图类型CollectionViewSource正在为您的幕后制作)。


I have the same problem like this. But I´m using a DataGrid instead of a ListBox and it does not seem to work like this (it might also be because i never used visual basic and didnt translate the code correcly into c#). I basicly want two DataGrids on the same data with different filters.

    ICollectionView view_dataLinesUnfiltered;
    ICollectionView view_dataLinesFiltered;

 public MainWindow()
    {
        ...
        //view_dataLines = CollectionViewSource.GetDefaultView(dataLines); // <- Filter works on both
        view_dataLinesUnfiltered = new CollectionView(dataLines); // <- Filter doesn´t work at all
        view_dataLinesFiltered = new CollectionView(dataLines);
        ....
        // Control Events
        this.ShowAA.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ToggleButton.UncheckedEvent));

    }

    private void ShowAA_Checked(object sender, RoutedEventArgs e)
    {
        view_dataLinesUnfiltered.Filter = null;
    }

    private void ShowAA_UnChecked(object sender, RoutedEventArgs e)
    {
        view_dataLinesUnfiltered.Filter = delegate(object o) { return FilterContent(o as ErrorDetection.stDataLine, "AA", ""); };
    }

    bool FilterContent(ErrorDetection.stDataLine line, string sFilterAA, string sFilter)
    {
        shortArrayToHexStringConverter converter = new shortArrayToHexStringConverter();

        string comBuffer = converter.Convert(line.ComBufferP as object,typeof(string),0,System.Globalization.CultureInfo.CurrentCulture) as string;

        return false;// !comBuffer.Contains("AA");
    }

The FilterContent method is being called without problems, but the DataGrid shows the lines anyway. If I use GetDefaultView the Filter works on both Datagrids. Do I have to use some other view instead of CollectionView (ListCollectionView does also not work)?

i have made a small sample project to show the problem sample. It only consists of an constructor and an observable collection.

解决方案

You need to specify which ICollectionVIew is used on which DataGrid.

If you just bind to the collection (dataLines in this case) WPF will use the 'default view' (or create one if necessary), this is why the first commented out line works for filtering.

There are a few ways you could specify which view is used for which datagrid, depending on what patterns, etc. you are using

1) Like the linked question, you could set the ItemsSource for each DataGrid in the window's code behind, after initializing the views, e.g.:

filteredDataGrid.ItemsSource = view_dataLinesFiltered;
unfilteredDataGrid.ItemsSource = view_dataLinesUnfiltered;

2) You could set the DataContext of the window to itself, or make a view model for the screen that contains the view, and make the views public properties and then bind to the intended view for each grid, e.g.

<DataGrid ItemsSource="{Binding View_dataLinesFiltered}"> ....


Edit:

Now I'm not at work and can get to dropbox and play with your example it seems like the cause of the weird behaviour is the use of CollectionView directly. On the msdn page for CollectionView it says

You should not create objects of this class in your code. To create a collection view for a collection that only implements IEnumerable, create a CollectionViewSource object, add your collection to the Source property, and get the collection view from the View property.

However, if you don't want to set up the views in XAML, you could also change your CollectionViews to ListCollectionViews and it should work as expected (this is likely the view type that CollectionViewSource is making for you behind the scenes anyway).

这篇关于Datagrid可观察的集合的多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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