我最近在这里抓住了这个项目:https://github.com/Nimgoble/WPFTextBoxAutoComplete它为WPF中的TextBoxes添加了自动完成行为。
您可以将此属性添加到TextBox以获取自动完成行为:behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding YourCollection}"
我试图让这个行为与DataGridTextColumn中的TextBox一起使用但没有成功。如何将此属性添加到DataGridTextColumn中包含的TextBox?
谢谢!
编辑:尝试制作一个DataTemplate列,仍然无效。
<DataGridTemplateColumn Header="Test Stuff">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
如果关闭我的DataGrid Binding可能会有什么问题?这是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>
答案 0 :(得分:2)
您应该使用该行为,可能是您遇到了行的DataContext问题。
按照此答案更新从DataGrid获取DataContext的行为绑定:Bind to DataContext Property from within DataGridColumn
答案 1 :(得分:0)
如果仍然有人对此主题感兴趣,可以使用一种简单的方法将自动完成功能绑定到datagridtextcolumn。 从https://github.com/Nimgoble/WPFTextBoxAutoComplete使用AutoCompleteBehavior 并使用https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
中的BindingProxy类...
<UserControl.Resources>
<mdv:BindingProxy x:Key="bindingproxy"
Data="{Binding}" />
</UserControl.Resources>
...
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<Setter Property="behaviors:AutoCompleteBehavior.AutoCompleteItemsSource"
Value="{Binding Data.md_AutoComplette.d_Items, Source={StaticResource bindingproxy}}" />
</Style>
</DataGridTextColumn.EditingElementStyle>
...