WPF DataGrid 如何循环遍历/选择具有特定属性的单元格?

5
刚接触WPF,对WinForms比较熟悉(这可能让转换更加艰难)。我正在尝试将旧的WinForms项目中的一些功能移植到WPF,以此来进行学习。
目标是在DataGrid中查找与TextBox中的字符串匹配的单元格值。我发现了一个使用绑定的很棒的示例,它可以实现这个目标。基本上,链接中的代码会将任何匹配的DataGridCell的背景颜色更改为橙色。我已经稍微修改了我的版本,但是功能应该是一样的。请参见链接以获取代码示例,在这里提供它似乎有点多余。填充我的DataGrid的数据来自于一个DataTable(如果有关系的话)。
从那里开始,我想做的是添加一个“下一个”按钮,它将循环遍历每个单元格(通过使用背景颜色或自定义属性DataGridTextSearch.IsTextMatch确定),并选择它。看起来可能只需对提供的代码进行一些修改就可以实现,但我不知道从哪里开始。在我的旧WinForms项目中,我使用Linq查询在列表中存储DataGridViewCell,并将按钮行为附加到递增所述列表并设置当前单元格上。我怀疑可能有一种更明智/更好的方法涉及绑定,如果这是一个选项,我甚至不知道如何将这些匹配单元格添加到列表中。
因此,总结一下,我想要一个按钮,它可以循环遍历特定的DataGridCells(基于Background或自定义的DataGridTextSearch.IsTextMatch属性)并选择它们。
提前致谢。
1个回答

5
根据您在问题中提供的链接,我已经为此找到了解决方案。在我的解决方案中,当DataGridCellTextBox中的字符串匹配时,它的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;
}

1
太棒了。我实际上正在做类似的事情,但是要花很长时间才能想出整个解决方案。感谢您!谢谢。 - Finch042

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接