我有一个WPF组合框......这是不可编辑的。当我选中这个组合框时...我有一个样式设定器(<Setter Property="IsDropDownOpen" Value="True"/>
)来打开组合框。但是当我再次选中时......焦点移动到打开的组合框中的下一个项目......并且它在那里循环。我无法选择下一个控件。
这里有什么问题?
由于
答案 0 :(得分:2)
尝试:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="IsTabStop" Value="False"/>
</Style>
或
使用KeyboardNavigation:
WPF tab order with custom controls?
不推荐,但有效...
<Grid>
<ComboBox Grid.Row="1" Margin="0,0,0,0" Name="comboBox1" HorizontalAlignment="Left" Width="120" Height="20" IsEditable="False" KeyDown="comboBox1_KeyDown" GotKeyboardFocus="comboBox1_GotKeyboardFocus" >
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsDropDownOpen" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBoxItem>Male</ComboBoxItem>
<ComboBoxItem>Female</ComboBoxItem>
<ComboBoxItem>Unknown</ComboBoxItem>
</ComboBox>
</Grid>
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
ComboBox cb = sender as ComboBox;
if (e.Key == Key.Tab && cb.IsDropDownOpen)
{
ComboBoxItem item = FocusManager.GetFocusedElement(Window.GetWindow(this)) as ComboBoxItem;
cb.SelectedItem = item;
cb.IsDropDownOpen = false;
e.Handled = true;
}
}
private void comboBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
ComboBox cb = sender as ComboBox;
cb.IsDropDownOpen = true;
}
答案 1 :(得分:0)
您可以通过以下代码实现相同的目标: -
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
IsDropDownOpen = true;
e.Handled = true;
}
}
现在,当焦点设置在ComboBox上时,您需要按Enter键打开下拉菜单,然后您可以使用向下关键字来遍历ComboBox项目。 要移至下一个控件,您需要按Tab键。
答案 2 :(得分:0)
我遇到了同样的问题,在XAML中解决了这个问题:
<Style x:Key="RadComboBoxItemStyle" TargetType="telerik:RadComboBoxItem">
<Setter Property="Focusable"
Value="False" />
<Setter Property="IsHitTestVisible"
Value="True" />