3

我有一个绑定到 Observable Members 集合的数据网格。现在我想通过输入成员名称在数据网格中查找成员。我试过 IsTextSearchEnable 属性,但它没有搜索。这是我的 xaml。

<DataGrid Name="dgOtherCharges" AutoGenerateColumns="False" RowHeight="25" Grid.Row="4" AlternatingRowBackground="{StaticResource AlternateRowBackgroundBrush}" Grid.ColumnSpan="3" IsTextSearchEnabled="True" TextSearch.Text="Name"
              CanUserAddRows="False" HeadersVisibility="Column" SelectionUnit="Cell" ItemsSource="{Binding Path=MembersCollection,Mode=TwoWay}"  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
              VerticalGridLinesBrush="{StaticResource GridLineColorBrush}" HorizontalGridLinesBrush="{StaticResource GridLineColorBrush}">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="DataGridCell.IsSelected" Value="True">
                            <Setter Property="Background" Value="{StaticResource ButtonSelectedBrush}"  />
                            <Setter Property="BorderBrush" Value="#A8E3FC" />
                            <Setter Property="Foreground" Value="Black" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>

            <DataGrid.Columns>

                <DataGridCheckBoxColumn Binding="{Binding IsCheck,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="50">
                    <DataGridCheckBoxColumn.ElementStyle>
                        <Style TargetType="CheckBox">
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridCheckBoxColumn.ElementStyle>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Header="Member Name" Binding="{Binding Name,Mode=TwoWay}" IsReadOnly="True" Width="*"/>


            </DataGrid.Columns>
        </DataGrid>

我需要一个快速的解决方案

4

2 回答 2

8

我今天偶然发现了这个问题,以找到解决同一问题的方法。虽然TextSearch在 2012 年显然不可用,但目前是可用的。

因此,为了将来参考,下面是我目前针对此问题的解决方案,并添加了三个属性IsTextSearchCaseSensitiveIsTextSearchEnabledTextSearch.TextPath.

<DataGrid ItemsSource="{Binding Path=Directories, Mode=OneWay}"
          SelectedItem="{Binding Path=SelectedDirectory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          EnableRowVirtualization="False"
          EnableColumnVirtualization="True"
          VirtualizingStackPanel.IsVirtualizing="False"
          VirtualizingStackPanel.VirtualizationMode="Standard"
          IsTextSearchCaseSensitive="False"
          IsTextSearchEnabled="True"
          TextSearch.TextPath="Name">
   <DataGrid.Columns>
      <DataGridTemplateColumn Header="Name" SortMemberPath="Name" />
   </DataGrid.Columns>
</DataGrid>
于 2014-08-21T13:26:55.283 回答
5
<TextBox TextChanged="TextBox_TextChanged"/>

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
      var view = CollectionViewSource.GetDefaultView((DataContext as MyViewModel).MembersCollection);
      view.Filter = o => (o as Member).Name.Contains((sender as TextBox).Text);
}

我希望它足够快:)

于 2012-12-11T08:23:39.283 回答