根据文本框中的文本类型进行过滤

时间:2013-09-09 05:38:40

标签: c# wpf xaml

我有一个textBox和一个列表框,通过以下代码绑定到数据表:

StudentTableAdapter studentTableAdapter = new StudentTableAdapter();
IMDataSet.StudentDataTable studentDataTable;
studentDataTable = studentTableAdapter.GetData();
mainGrd.DataContext = studentDataTable;

并遵循xaml:

<!-- Student Data Template-->
    <DataTemplate x:Key="StudentDataTemplate" DataType="local:IMDataSet+StudentRow" >
        <DataTemplate.Resources>
            <local:PhoneNumberConverter x:Key="phoneNumConv"/>
            <local:SSIDConverter x:Key="SSIDNum"/>
            <local:GenderConverter x:Key="genderConverter"/>
            <local:DateToAgeConverter x:Key="dateToAgeConverter"/>
            <local:ShamsiConverter x:Key="shamsiConverter"/>
        </DataTemplate.Resources>

        <Grid Name="grdContainer" HorizontalAlignment="Right" MaxWidth="800" MinWidth="300" Margin="15" >
            <Grid.RowDefinitions>
                <RowDefinition Height="45"/>
                <RowDefinition Height="70"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110"/>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <!--Drop Shadow-->
            <Border x:Name="BrdShadow"  BorderBrush="#FFC1C1C1" BorderThickness="1" Margin="0" CornerRadius="4" Grid.ColumnSpan="2" Grid.RowSpan="2">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="18" Direction="0" ShadowDepth="0"/>
                </Border.Effect>
            </Border>

            <Border x:Name="BrdMainFrame" BorderBrush="#FFC1C1C1" BorderThickness="1" Margin="0" CornerRadius="4" Grid.ColumnSpan="2" Grid.RowSpan="3">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="WhiteSmoke"/>
                        <GradientStop Color="#FFF8F8F8" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
            </Border>

            <!--Image Frame-->
            <Border Padding="7" Margin="10" x:Name="BrdImageFrame"  BorderThickness="0.5" BorderBrush="white" CornerRadius="0,0,2,2" Grid.RowSpan="2" Background="White">
                <Border.Effect>
                    <DropShadowEffect BlurRadius="5" Direction="0" ShadowDepth="0"/>
                </Border.Effect>
                    <Path Data="M40.110036,42.607C40.110036,42.607,56.960053,46.620123,58.570054,62.667002L0,62.667002C0,62.667002,4.8100042,45.013212,19.250017,43.411707L23.170021,60.160589 35.400033,60.058988z M8.3739254,8.8940056C8.771577,8.8923805,9.0077471,9.656657,9.0077469,9.656657L10.117005,26.680164C8.2182753,29.703531 5.590034,26.749164 5.5900338,26.749164 5.590034,26.749164 6.9691112,10.990641 7.7385967,9.5146587 7.9790604,9.053726 8.193175,8.8947437 8.3739254,8.8940056z M30.645947,0L48.956487,5.4750259C49.786473,5.8140242,49.346517,6.3310254,49.346517,6.3310254L38.196124,9.2290428 38.196124,10.503051C38.256185,10.784051 37.996165,10.95305 37.786077,11.047052 42.156217,13.940064 45.036317,18.895893 45.036317,24.532617 45.036317,33.462362 37.796086,40.702001 28.865859,40.702001 19.94552,40.702001 12.705291,33.462362 12.705291,24.532617 12.705291,18.882892 15.605412,13.918069 19.995571,11.026048 19.525463,10.810052 19.465525,10.503051 19.465525,10.503051L19.465525,8.5030411 8.6151904,5.2530244C7.8551557,4.9480226,8.6252006,4.8330243,8.6252009,4.8330243z" Stretch="Uniform"  Fill="#FF000000" Margin="3"   />
            </Border>

            <StackPanel Margin="1" Grid.Column="1" Grid.RowSpan="2">
                <StackPanel Orientation="Horizontal" Margin="0,0,5,0">
                    <TextBlock Name="BlkGender" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Titr" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=Gender, Converter={StaticResource genderConverter}}" VerticalAlignment="Center" Margin="0,0,7,-5"  />
                    <TextBlock x:Name="BlkFullName" TextWrapping="Wrap" FontSize="26.667" FontFamily="/IM;component/Fonts/#Titr" Foreground="{DynamicResource HighImportanceText}" VerticalAlignment="Center"  >
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} {1}">
                                <Binding Path="Name"/>
                                <Binding Path="Family"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                    <TextBlock Name="blkAge" Text="{Binding Path=BornDate, Converter={StaticResource dateToAgeConverter }, StringFormat={} - {0} ساله}" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}"  ToolTip="{Binding Path=BornDate, Converter={StaticResource shamsiConverter}}" VerticalAlignment="Center" TextAlignment="Center" Margin="0,9,0,0"  />
                </StackPanel>

                <TextBlock Name="blkFather" TextWrapping="Wrap" FontSize="20" Foreground="{DynamicResource HalfImportanceText}"  Margin="20,2,0,0" FontFamily="/IM;component/Fonts/#Mitra"   Text="{Binding Path=FatherName, StringFormat=فرزند: {0}}" />
                <TextBlock Name="blkDefContact" TextWrapping="Wrap" FontSize="20" Foreground="{DynamicResource HalfImportanceText}"  Margin="20,2,0,0" FontFamily="/IM;component/Fonts/#Mitra"  Text="{Binding Path=DefaultContact, Converter={StaticResource phoneNumConv}}" FlowDirection="LeftToRight" HorizontalAlignment="Left" />
            </StackPanel>

            <Expander x:Name="expander" IsExpanded="False" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Grid.Row="2" Grid.ColumnSpan="2" Style="{DynamicResource ExpanderStyle1}" ExpandDirection="Down" >
                <StackPanel Margin="10,10,10,15" x:Name="stkFinName">
                    <TextBlock x:Name="FinName" TextWrapping="Wrap" FontSize="20" FontFamily="Calibri" FontWeight="Bold" Foreground="{DynamicResource HalfImportanceText}"  >
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} {1}">
                                <Binding Path="EnglishName"/>
                                <Binding Path="EnglishFamily"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>

                    <StackPanel Orientation="Horizontal" Margin="8,5,5,0">
                        <TextBlock x:Name="blkBornLoc" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=BornLocation, StringFormat=تولد: {0}}" Margin="0,0,5,0"  />
                        <TextBlock x:Name="blkSSID" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Path=SSID, Converter={StaticResource SSIDNum}}" Margin="5,0,0,0" FlowDirection="LeftToRight" HorizontalAlignment="Left"   />
                    </StackPanel>
                    <TextBlock x:Name="blkLicense" TextWrapping="Wrap" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding License}" Margin="10,0,10,0"  />
                    <TextBlock x:Name="blkAddress" TextWrapping="Wrap" MaxWidth="300" HorizontalAlignment="Left" FontSize="20" FontFamily="/IM;component/Fonts/#Mitra" Foreground="{DynamicResource HalfImportanceText}" Text="{Binding Address}" Margin="10,5,10,0"  />
                </StackPanel>
            </Expander>
        </Grid>
    </DataTemplate>

<!--TEXT BOX-->
            <TextBox x:Name="textBox" TextWrapping="Wrap" Style="{DynamicResource SearchBoxStyle}" VerticalAlignment="Center" Margin="10,0,0,0" Height="43" FontSize="24" Width="516.11" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" FontFamily="/IM;component/Fonts/#Mitra" >
                <TextBox.BorderBrush>
                    <LinearGradientBrush EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
                        <GradientStop Color="#FFA7A7A7" Offset="0.05"/>
                        <GradientStop Color="#FFBABABA" Offset="0.07"/>
                        <GradientStop Color="#FFBABABA" Offset="1"/>
                    </LinearGradientBrush>
                </TextBox.BorderBrush>
            </TextBox>

<--LIST BOX-->
        <ListBox Name="lstItems" TextSearch.TextPath="MaterialName" Style="{StaticResource PhotoListBoxStyle}" Grid.Row="2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" ItemsSource="{Binding}" ItemTemplate="{DynamicResource StudentDataTemplate }"  />

当用户写一个数字时我想过滤listBox By DefaultContact属性。 当他写一个单词过滤它的名字属性。 我该怎么做? 提前致谢。

1 个答案:

答案 0 :(得分:0)

几天前我就给出了类似要求的答案。看看它能不能帮到你..

WPF - Filtering a DataCollection with an autocompletebox

由于