我需要一个从win run
窗口极端看起来像组合框的控件。
我的问题是它是具有特定设置的标准combobox control
还是我必须创建custom control
以实现此外观和行为?
我对第三张图片旁边的外观和行为感兴趣 - 就像在按下某个键后在listbox
中显示为popup
的过滤建议一样。
答案 0 :(得分:1)
这已内置于ComboBox
。这是一个例子:
<ComboBox
IsEditable="True" //This enables to enter values that don't belong to the ItemsSource collection
IsTextSearchEnabled="True" //this allows you to have "suggestions" when you enter text
IsTextSearchCaseSensitive="False"
StaysOpenOnEdit="True"
Text="{Binding NameOnViewModel}"
TextSearch.TextPath="NameOnChildItems" //this is the property on which combobox will filter the items
ItemsSource="{Binding Items}" //collection of your items to search on
ItemTemplate="{StaticResource DataTemplate}" />//this can be replaced with DisplayMemeberPath="PropertyName"
注意:
此示例取自this SO post
的修改
如果您希望在键入值时打开弹出窗口,那么这可能会有所帮助:
</ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">//you can also use a different event if this one doesn't suit your needs.
<Setter Property="IsDropDownOpen" Value="true" />
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
编辑2
要在组合框的下拉列表中过滤VISIBLE对象,请使用按键事件并在事件处理程序中对其进行过滤,如下所示:
private void cmbKeyDown(object sender, KeyEventArgs e)
{
string temp = ((ComboBox)sender).Text;
var newList = MyList.Where(x => x.Name.Contains(temp));
MyList = newList.ToList();
}