简体   繁体   English

组合框没有得到关注

[英]Combobox doesn't get focus

I have a non-editable combobox ( SupplierDropdown ), and I would like when the user chooses the last value on the list, the combobox should become editable, with no value and automatically focused ready for the user to type their value. 我有一个不可编辑的组合框( SupplierDropdown ),我希望当用户选择列表上的最后一个值时,该组合框应变为可编辑的,没有任何值,并自动聚焦以供用户键入其值。 In other words, an editable, blank box with a blinking cursor ready for input. 换句话说,一个可编辑的空白框带有闪烁的光标,可供输入。

Here's the code I have: 这是我的代码:

private void SupplierDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (SupplierDropdown.SelectedIndex == SupplierDropdown.Items.Count - 1)
        {
            SupplierDropdown.IsEditable = true;
            SupplierDropdown.Text = "";
            SupplierDropdown.Focus();
        }
    }

However, although the combobox indeed becomes editable, the rest of the code doesn't seem to work: (a) the combobox doesn't clear the value the user chose, and (b) the combobox doesn't get the focus, the user needs to hit tab once to see the blinking cursor in the combobox. 但是,尽管组合框确实可以编辑,但其余代码似乎不起作用:(a)组合框无法清除用户选择的值,并且(b)组合框无法获得焦点,用户需要点击一次标签,才能在组合框中看到闪烁的光标。

How can I make this work? 我该如何进行这项工作?

This seems like a major pain, because the control just is not intended to be used that way. 这似乎是一个很大的痛苦,因为该控件不打算以这种方式使用。 Even if you get the initial logic to work, it is difficult to turn the editable flag back off in a sound way. 即使您可以使用初始逻辑,也很难以合理的方式关闭可编辑标志。 When the user enters text that partially matches a given option it will select said option. 当用户输入与给定选项部分匹配的文本时,它将选择所述选项。 How do you know whether that selection was caused by accident or intentionally and thus whether the editable flag should be set to false again? 您怎么知道该选择是偶然还是有意引起的,因此是否应将可编辑标志再次设置为false?

Would recommend the additional TextBox scheme, where you just show a TextBox for any custom values, can be done in XAML only (if you know what value to trigger on) but that's not necessary. 建议使用其他TextBox方案,仅在其中显示任何自定义值的TextBox ,只能在XAML中完成(如果您知道要触发的值),但这不是必需的。

<ComboBox Name="cb" SelectedValuePath="Content">
    <ComboBoxItem>A</ComboBoxItem>
    <ComboBoxItem>B</ComboBoxItem>
    <ComboBoxItem>Other</ComboBoxItem>
</ComboBox>
<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedValue, ElementName=cb}" Value="Other">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

(PS: This is an example of using the item/value instead of the index, even if someone decides that the "other" option should be at the top instead, the code will still work as intended, yours would not.) (PS:这是一个使用项目/值而不是索引的示例,即使有人决定将“其他”选项放在顶部,该代码仍将按预期工作,而您不会。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM