根据WPF中的文本框文本过滤列表框

时间:2012-11-21 13:02:14

标签: wpf vb.net linq filtering

我想知道我是否能得到这方面的帮助。

我正在使用包含2个表(dbo.Project和dbo.Materials)和collectionview的数据集将数据显示到列表框中。
我需要通过使用两个文本框按项目中使用的材料过滤数据集,并将项目名称返回到列表框中。到目前为止,我已经将它添加到它加载的位置,我的列表框正确填充了项目列表,但文本框没有过滤。

非常感谢任何有关如何修复它的建议。

我到目前为止的代码是:

Imports System.Windows.Data
Imports System.Windows

Public Class SearchMe
    Private mds As New MaterialsDataSet
    Private pta As New MaterialsDataSetTableAdapters.ProjectTableAdapter
    Private mta As New MaterialsDataSetTableAdapters.MaterialsTableAdapter
    Private tam As New MaterialsDataSetTableAdapters.TableAdapterManager
    Private view As CollectionView

    Private Sub SearchMe_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Me.tam.ProjectTableAdapter = pta
        Me.tam.MaterialsTableAdapter = mta

        Me.pta.Fill(Me.mds.Project)
        Me.mta.Fill(Me.mds.Materials)

        Me.DataContext = Me.mds.Project
        Me.view = CollectionViewSource.GetDefaultView(Me.mds.Project)
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnSearch.Click
        If Material2.Text.Length > 0 Then
            Dim query = From p In mds.Project
                        Join m In mds.Materials On p.ProjectID Equals m.ProjectID
                        Where m.Material = Me.Material2.Text.ToString
                        Select p
        Else
            Dim query = From p In mds.Project
                        Join m In mds.Materials On p.ProjectID Equals m.ProjectID
                        Where m.Material = Me.Material1.Text
                        Select p.ProjectName

            Me.DataContext = query
        End If
    End Sub

    Private Sub btnReset_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles btnReset.Click
        Me.DataContext = Nothing
    End Sub

    Private Sub Material1_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles Material1.TextChanged

    End Sub

    Private Sub Material2_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles Material2.TextChanged

    End Sub
End Class

和XAML:

<Window x:Class="SearchMe"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SearchMe" Height="500" Width="750" WindowStartupLocation="CenterScreen" ShowInTaskbar="False" ShowActivated="False" ResizeMode="NoResize">
<Grid>
    <ListBox HorizontalAlignment="Right" Margin="0,45,46,60" Name="MyLists" Width="346" BorderThickness="3" BorderBrush="#FF860909"
             IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}"   
             DisplayMemberPath="ProjectName"/>
    <Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="403,417,0,0" Name="btnSearch" VerticalAlignment="Top" Width="75" />
    <Button Content="Reset" Height="23" HorizontalAlignment="Left" Margin="556,417,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" />
    <TextBox Height="30" HorizontalAlignment="Left" Margin="29,87,0,0" Name="Material1" VerticalAlignment="Top" Width="269" />
    <TextBox Height="30" HorizontalAlignment="Left" Margin="29,192,0,0" Name="Material2" VerticalAlignment="Top" Width="269" />
</Grid>

任何和所有输入都赞赏。

2 个答案:

答案 0 :(得分:0)

这里可能存在许多潜在问题,我没有时间仔细阅读您的代码。但是在替换click事件中的DataContext之后,您可以尝试在“Me.view”上调用Refresh方法。

答案 1 :(得分:0)

好的,这对我有用...在btn_Search Click事件下:

Dim query = From p In mds.Project
                Join m In mds.Materials On p.ProjectID Equals m.ProjectID
                Where m.Materia.ToLower().Contain(m.Material1.Text())
                Select new with {.ProjectName = p.ProjectName}

希望这可以帮助任何人。