【问题标题】:WPF Datagrid Cycle Through/Select Cells With Specific PropertyWPF Datagrid 循环/选择具有特定属性的单元格
【发布时间】:2016-01-11 16:54:02
【问题描述】:

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

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

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

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

提前致谢。

【问题讨论】:

    标签: .net wpf binding datagrid


    【解决方案1】:

    根据您在问题中提供的link,我已经找到了解决方案。在我的解决方案中,当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;
    }
    

    【讨论】:

    • 太棒了。我实际上是在做类似的事情,但是我要花很长时间才能想出整个解决方案。欣赏它!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 2017-11-29
    • 2016-08-27
    • 2015-05-29
    • 1970-01-01
    • 2019-05-14
    • 2013-06-08
    相关资源
    最近更新 更多