我正在尝试构建一个commbobox过滤器,如下所述: http://andora.us/blog/2012/01/25/filtering-items-in-a-wpf-combobox/
我已按照此处所述的示例进行操作,但当窗口中有多个组合框时,它不起作用。如果我在一个组合框中应用滤镜,则在所有其他框中应用相同的滤镜。如何设置此选项,以便在每个组合框中使用单独的滤镜?
此外 - 当我尝试选择已过滤列表中的某个项目时,它并不总是填充在comobobox中。
或许对此有不同/更好的解决方案?
The first part of it was creating a new Style for the ComboBox, so that inside the Popup (PART_Popup) portion of the ControlTemplate can include my filtering TextBox.
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<DockPanel LastChildFill="True" Margin="0,5,10,5" Grid.Row="0">
<TextBlock DockPanel.Dock="Left" Text="Filter:" Margin="0,0,5,0" />
<TextBox x:Name="DropDownFilterTextBox" />
</DockPanel>
<ScrollViewer x:Name="DropDownScrollViewer" Grid.Row="1">
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Grid>
</Grid>
</Border>
</Microsoft_Windows_Themes:SystemDropShadowChrome>
另一部分是在过滤器TextBox中键入ComboBox内的项目。为此,我将附加到TextChanged事件上。
protected void DropDownFilterTextBox_TextChanged(object sender,TextChangedEventArgs e) { TextBox textBox =((TextBox)sender);
ComboBox cbo = ((TextBox)sender).TemplatedParent as ComboBox;
cbo.Items.Filter += a =>
{
if (a.ToString().ToLower().StartsWith(textBox.Text.ToLower()))
{
return true;
}
return false;
};
}