在 DataGrid 中使用 WPF 文本框自动完成 [英] Using WPF TextBox Autocomplete in a DataGrid

查看:34
本文介绍了在 DataGrid 中使用 WPF 文本框自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在此处获取了该项目:https://github.com/Nimgoble/WPFTextBoxAutoComplete 它添加了WPF 中文本框的自动完成行为.

I recently grabbed the project here: https://github.com/Nimgoble/WPFTextBoxAutoComplete it adds autocomplete behavior to TextBoxes in WPF.

您将此属性添加到自动完成行为的文本框:behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding YourCollection}"

You add this property to a TextBox for the AutoComplete behavior: behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding YourCollection}"

我试图让行为与 DataGridTextColumn 中的 TextBoxes 一起工作,但没有成功.如何将此属性添加到 DataGridTextColumn 中包含的 TextBox?

I'm trying to get the behavior to work with the TextBoxes in a DataGridTextColumn with no success. How do I add this property to the TextBox contained within the DataGridTextColumn?

谢谢!

尝试制作一个 DataTemplate 列,仍然没有工作.

Tried making a DataTemplate Column, still did not work.

        <DataGridTemplateColumn Header="Test Stuff">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems,  RelativeSource={RelativeSource AncestorType=DataGrid}}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

如果关闭我的 DataGrid 绑定,可能会发生什么?这是数据网格:

Maybe something if off on my DataGrid Binding? Here is the DataGrid:

<DataGrid ItemsSource="{Binding UsersList.Users}"
                      AutoGenerateColumns="False"
                      GridLinesVisibility="All"
                      FontSize="12"
                      Margin="0"
                      HorizontalAlignment="Center"
                      BorderThickness="0">
                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}"
                           BasedOn="{StaticResource MetroDataGridRow}">
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridCell}">
                                    <Grid Background="{TemplateBinding Background}">
                                        <ContentPresenter VerticalAlignment="Center" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Name}"
                                        ClipboardContentBinding="{x:Null}"
                                        behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems,  RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                        Header="Name" />
                    <DataGridTextColumn Binding="{Binding ID}"
                                        ClipboardContentBinding="{x:Null}"
                                        Header="User ID" />
                    <DataGridCheckBoxColumn Binding="{Binding Valid}"
                                            ElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                            ClipboardContentBinding="{x:Null}"
                                            Header="Valid Name" />
                    <DataGridTemplateColumn Header="Test Stuff">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems,  RelativeSource={RelativeSource AncestorType=DataGrid}}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

推荐答案

您应该使用该行为,可能您的行的 DataContext 有问题.

You should use the behavior, probably you are facing a problem with the DataContext of your row.

按照此答案更新您的行为绑定,从 DataGrid 获取 DataContext:从 DataGridColumn 中绑定到 DataContext 属性

Follow this answer to update your behavior binding taking the DataContext from the DataGrid : Bind to DataContext Property from within DataGridColumn

这篇关于在 DataGrid 中使用 WPF 文本框自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆