C# WPF DataTable 到 CollectionViewSource 中,用于 ViewList [英] C# WPF DataTable into a CollectionViewSource use that for a ViewList

查看:29
本文介绍了C# WPF DataTable 到 CollectionViewSource 中,用于 ViewList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从数据库中获取了数据,现在我想放入一个 CollectionViewSource,然后将其用作 ListView 的源.我想这样做,以便我可以过滤 ListView.但是我不明白如何将数据表从 c# 传递到 XAML CollectionViewSource?

I'm have gotten data from a database, now I want to put into a CollectionViewSource and then use that as the source for a ListView. I want to do this so I can filter the ListView. However I don't understand how pass the Datatable from c# to the XAML CollectionViewSource?

<Window.Resources>
    <ResourceDictionary >
        <CollectionViewSource x:Key="NamesOfCompany"/>
    </ResourceDictionary>
</Window.Resources>

<ListView x:Name="listView" ...
          ItemsSource="{Binding Source={StaticResource NamesOfCompany}}" >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="CompanyName" 
                DisplayMemberBinding="{Binding CompanyName}" />
        </GridView>
   </ListView.View>
</ListView>

<Grid Margin="2,441.5,-2,0" Grid.Row="1" >
   <TextBox x:Name="txt_search" Height="22" TextWrapping="Wrap" Text="{Binding TextSearch,UpdateSourceTrigger=PropertyChanged}" Width="234" Margin="0,0,451,8" TextChanged="txt_search_TextChanged" HorizontalAlignment="Right" VerticalAlignment="Bottom" VerticalContentAlignment="Center"/>
</Grid>

我的数据库中的所有数据都保存在

All my data from my database is held in

private DataTable dt;

我不确定您如何将 dt 中的所有数据分配到 CollectionViewSource 中.

I'm not sure how you assign all the data from dt into the CollectionViewSource.

推荐答案

您不需要 collectionviewsource 来绑定到数据表.只需将数据表直接连接到 ItemsSource.一种方法是在后面的代码中...

You don't need a collectionviewsource to bind to datatable. Simply connect the datatable directly to the ItemsSource. One way to do this is in the code behind...

DataView dv = new DataView();
dv.Table = dt;
dv.Filter = "CompanyName='abc'";
listView.ItemsSource = dv;

这篇关于C# WPF DataTable 到 CollectionViewSource 中,用于 ViewList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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