Datagrid标头中的WPF自动完成文本框 [英] WPF AutoComplete TextBox in Datagrid Header
问题描述
你好
我有一个要求,我应该在Datagrid标头中有一个文本框,该文本框充当自动完成文本框.任何人都可以提供示例代码的帮助.
I got a requirement where I should have a textBox in Datagrid header which acts as a AutoComplete TextBox. Could anyone help with sample code.
此致
Padma
推荐答案
WPF中没有标准的自动填充文本框,您必须使用WPF工具包中的文本框,第三方供应商(例如Telerik),社区版本或您自己建立. http://www.codeproject.com/Articles/293954/AutoSuggest-and-AutoComplete-control-in-WPF
There is not a standard autocomplete textbox in WPF, you will have to use the one in the WPF Toolkit, a third party supplier like Telerik, a community version or build your own. http://www.codeproject.com/Articles/293954/AutoSuggest-and-AutoComplete-control-in-WPF
下面显示了如何将所有这些连接起来,在这种情况下使用标准TextBox.然后,您只需要弄清楚如何将其从DataGridColumnHeader这样的远程位置绑定回源.如果您使用代码隐藏的textchanged事件,则 只需为您的文本框指定名称,例如TextBoxColumn_1,TextBoxColumn_2,即可从事件发送者的名称中获取列号.
Below shows HOW you wire it all up, using in this case a standard TextBox. You then just need to work out how you're going to bind it from such a remote place as the DataGridColumnHeader, back to your source. If you use code-behind textchanged event, then simply give your textboxes names like TextBoxColumn_1, TextBoxColumn_2, so you can get the column number out of the event sender's name.
<Window.Resources>
<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid VerticalAlignment="Center" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="" HorizontalAlignment="Stretch"/>
<Grid Grid.Row="1">
<TextBox Text="" HorizontalAlignment="Stretch" BorderThickness="1" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<DataGrid x:Name="dataGrid" Height="157" Width="600" Margin="8,8,24,0"
VerticalAlignment="Top"
AutoGenerateColumns="False"
ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"
ItemsSource="{Binding}" CanUserAddRows="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Header1" Binding="{Binding Id}" Width="100" />
<DataGridTextColumn Header="Header2" Binding="{Binding Name}" Width="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
祝你好运!
Pedro
这篇关于Datagrid标头中的WPF自动完成文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!