.net - WPF Datagrid 循环/选择具有特定属性的单元格

标签 .net wpf binding datagrid

对 WPF 来说是全新的,对 WinForms 非常满意(这可能会使过渡更加粗糙)。我正在尝试将旧 WinForms 项目中的一些功能移植到 WPF 作为学习体验。

目标是在 DataGrid 中查找与 TextBox 中的字符串匹配的单元格值。我找到了 great example使用可以做到这一点的绑定(bind)。基本上,链接代码会将任何匹配的 DataGridCell 的背景颜色更改为橙​​色。我稍微修改了我的版本,但功能应该是一样的。请参阅代码示例的链接,在这里提供它似乎有点多余。填充我的 DataGrid 的数据来自 DataTable(如果重要的话)。

我想要从那里做的是有一个“下一步”按钮,它将循环通过每个单元格(通过使用背景颜色或自定义属性 DataGridTextSearch.IsTextMatch 确定)并选择它。似乎可以只修改一些提供的代码,但我不知道从哪里开始。在我的旧 WinForms 项目中,我将 DataGridViewCell 存储在一个列表中(在使用 Linq 查询找到它们之后),并将按钮行为附加到递增所述列表并设置当前单元格。我怀疑可能有一种更智能/更好的方式涉及绑定(bind),如果可以的话,我什至不知道如何将这些匹配的单元格添加到列表中。

所以,总而言之,我想要一个循环通过特定 DataGridCells(基于背景或自定义 DataGridTextSearch.IsTextMatch 属性)并选择它们的按钮。

提前致谢。

最佳答案

基于link你在你的问题中提供了,我已经找到了解决方案。在我的解决方案中,当 DataGridCell匹配 TextBox 中的字符串,它是 Tag属性将设置为“1”,然后当 Button被点击,它将遍历所有 DataGridCells并查找具有非空 Tags 的项目最后突出显示的单元格将被一一聚焦。

这是一个工作示例,可以为您提供一个想法:

Xaml:

<Window Name="UI">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel DataContext="{Binding ElementName=UI}" Grid.Row="0">
            <TextBox Name="SearchBox" TextChanged="SearchBox_TextChanged"/>
            <DataGrid x:Name="grid" 
                  m:DataGridTextSearch.SearchValue="{Binding ElementName=SearchBox, Path=Text, UpdateSourceTrigger=PropertyChanged}" 
                  ItemsSource="{Binding TestData}"
                  SelectionUnit="Cell">
                <DataGrid.Resources>
                    <m:SearchValueConverter x:Key="SearchValueConverter" />
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="m:DataGridTextSearch.IsTextMatch">
                            <Setter.Value>
                                <MultiBinding Converter="{StaticResource SearchValueConverter}">
                                    <Binding RelativeSource="{RelativeSource Self}" Path="Content.Text" />
                                    <Binding RelativeSource="{RelativeSource Self}" Path="(m:DataGridTextSearch.SearchValue)" />
                                </MultiBinding>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="m:DataGridTextSearch.IsTextMatch" Value="True">
                                <Setter Property="Background" Value="Orange" />
                                <Setter Property="Tag" Value="1" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.Resources>
            </DataGrid>
        </StackPanel>
        <Button Grid.Row="1" Click="Button_Click" Content="GoNext"/>
    </Grid>
</Window>

MainWindow.cs:
int currentIndex = 0;

private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
    currentIndex = 0;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    var selectedCells = GetHighLightedCells();
    if (selectedCells.Count == 0)
        return;

    selectedCells[currentIndex].Focus();

    if (currentIndex == selectedCells.Count - 1)
        currentIndex = 0;
    else
        currentIndex++;
}

获取高亮单元格的方法:
public List<DataGridCell> GetHighLightedCells()
{
    List<DataGridCell> selectedCells = new List<DataGridCell>();
    foreach (DataGridRow rowContainer in GetDataGridRows())
    {
        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
            foreach (var col in grid.Columns)
            {
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(col.DisplayIndex);
                if (cell == null)
                {
                    grid.ScrollIntoView(rowContainer, col);
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(col.DisplayIndex);
                }
                if (cell.Tag != null)
                {
                    selectedCells.Add(cell);
                }
            }
        }
    }
    return selectedCells;
}
public IEnumerable<DataGridRow> GetDataGridRows()
{
    var itemsSource = grid.ItemsSource as IEnumerable;
    if (null == itemsSource) yield return null;
    foreach (var item in itemsSource)
    {
        var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
        if (null != row) yield return row;
    }
}

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}

关于.net - WPF Datagrid 循环/选择具有特定属性的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33111144/

相关文章:

c# - 正则表达式前瞻在 .NET 中不起作用

c# - 尝试利用通用参数的组合

WPF MenuItem.Command 绑定(bind)到 ElementName 结果为 System.Windows.Data 错误 : 4 : Cannot find source for binding with reference

c# - stackpanel 中动态添加的控件在 wpf c# 中不可见

asp.net-mvc - 程序集绑定(bind)重定向不起作用

c# - SGEN : An attempt was made to load an assembly with an incorrect format

c# - 从 'System.Int32' 到 'System.Nullable` 1[[System.Int32, mscorlib]] 的转换无效

c# - 序列化 RichTextBox,包括大小、背景颜色、位置

javascript - 将两个选择框值绑定(bind)到单个 knockout 日期属性

mvvm - SwiftUI - @Binding 到访问 ObservableObject 属性内的值的计算属性会重复变量吗?