1

我想知道我是否可以得到一些帮助。

我正在使用包含 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>

任何和所有输入表示赞赏。

4

2 回答 2

0

这里可能有很多潜在的问题,我没有时间详细浏览你的代码。但是您可以尝试在单击事件中替换 DataContext 后在“Me.view”上调用 Refresh 方法。

于 2012-11-21T13:24:22.920 回答
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}

希望这可以帮助任何人。

于 2012-12-12T16:23:52.887 回答