通过键入在 Wpf 数据网格中查找记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13815607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find a record in Wpf datagrid by typing
提问by lordhusnain
I have a datagrid which is bound to Observable Members collection. Now i want to find a member in datagrid by typing the member name. I have tried IsTextSearchEnable property but it is not searching. Here is my xaml.
我有一个绑定到 Observable 成员集合的数据网格。现在我想通过键入成员名称在数据网格中查找成员。我试过 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>
i need a quick solution folks
我需要一个快速的解决方案
回答by Herman Cordes
I stumbled upon this question today to find a solution to the same problem. While TextSearchapparently was not available back in 2012, it currently is.
我今天偶然发现了这个问题,以找到解决同一问题的方法。虽然TextSearch显然在 2012 年不可用,但目前可用。
So for future reference, below my current solution for this problem with three added properties: IsTextSearchCaseSensitive, IsTextSearchEnabledand TextSearch.TextPath.
因此,为了将来参考,在我针对此问题的当前解决方案下方添加了三个附加属性:IsTextSearchCaseSensitive,IsTextSearchEnabled和TextSearch.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>
回答by Dtex
<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);
}
I hope it was quick enough :)
我希望它足够快:)

