我有一个绑定到“可观察成员”集合的数据网格。现在,我想通过键入成员名称在datagrid中找到成员。我已经尝试过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>
我需要一个快速解决方案的人
最佳答案
<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);
}
我希望它足够快:)