简体   繁体   English

在WPF中未设置组合框的选定文本

[英]unset selected text of combobox in WPF

I have a WPF app with a combobox that is bound to a database call. 我有一个WPF应用程序,该应用程序具有一个绑定到数据库调用的组合框。 The user types a name, and the database call returns the possible name matches. 用户键入一个名称,数据库调用将返回可能的名称匹配。 I check the length of the typed text to make sure at least 4 characters have been typed to increase speed. 我检查输入文字的长度,以确保至少输入了4个字符以提高速度。

The issue i have is that after the database call returns its result, the text in the combobox is selected. 我的问题是在数据库调用返回其结果后,组合框中的文本被选中。 The user continues to type at this point they overwrite the selected text. 此时,用户继续输入内容,他们将覆盖所选的文本。 I tried many things, but setting the selected index in code-behind gives partial results. 我尝试了很多事情,但是在代码隐藏中设置所选索引会产生部分结果。 initially it doesn't work but subsequent searches the text is not selected. 最初它不起作用,但随后的搜索未选择该文本。 Ideally i would like to set something like selectionLength = 0 but it isn't available to me. 理想情况下,我想设置诸如selectionLength = 0之类的内容,但它对我不可用。

My question is how do i stop the combobox text already entered from becoming selected? 我的问题是如何阻止已输入的组合框文本被选中?

the xaml extract: xaml提取物:

<ComboBox 
            Grid.Column="2" 
            Grid.Row="1" 
            Height="30" 
            IsEditable="True"
            IsReadOnly="False"
            ItemsSource="{Binding PatientsCollection,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}"
            Name="SelectPatientCombobox" 
            SelectedValue="{Binding SelectedPatientId,Mode=Default}"
            SelectedValuePath="pat_pharmacy_patient_id" 
            TargetUpdated="SelectPatientCombobox_TargetUpdated"
            Text="{Binding SearchPatientText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            VerticalAlignment="Top"
            Visibility="{Binding SelectPatientVisible, Converter={StaticResource BoolToVis}}"
            >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,0,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Image Source="{Binding Path=PatientIcon}" MaxHeight="32" MaxWidth="32" Margin="0,2,5,2"
                           Grid.Column="0" Grid.RowSpan="2" />
                        <TextBlock Grid.Row="0" Grid.Column="1">
                        <Run Text="{Binding TitleFirstName, Mode=OneWay}" />
                        <Run Text=" " />
                        <Run FontWeight="Bold" Text="{Binding pat_name_last}" />
                        </TextBlock>
                        <StackPanel Grid.Row="1" Orientation="Horizontal" Grid.Column="1">
                            <TextBlock Foreground="SlateGray" Text="{Binding cod_addr1}" Margin="0,0,5,0" />
                            <TextBlock Foreground="SlateGray" Text="{Binding suburb}" Margin="0,0,5,0" />
                            <TextBlock Foreground="SlateGray" Text="{Binding postcode}" Margin="0,0,0,0" />
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

the code behind: 后面的代码:

        private void SelectPatientCombobox_TargetUpdated(object sender, DataTransferEventArgs e)
    {
        if ( false == ((ComboBox) sender).IsLoaded) return;
        ((ComboBox) sender).IsDropDownOpen = ((ComboBox) e.Source).Items.Count > 0;
        ((ComboBox)sender).SelectedIndex = -1;
    }

The ViewModel complicated when the database call returns data it converts it to an ObservableCollection 当数据库调用返回数据时,ViewModel变得复杂,它将其转换为ObservableCollection

answering this if someone else has the same issue. 如果其他人有相同的问题,请回答此问题。 This is a hack, but i couldn't work out a better way to fix the issue properly. 这是一个hack,但是我无法找到更好的方法来正确解决此问题。

In the code-behind i programmatically press the right arrow key to make the text not selected. 在我后面的代码中,以编程方式按右箭头键使文本未被选中。 There is a slight blip of the screen where you can see the the text being unselected, but it works. 屏幕略微出现一点,您可以看到未选中的文本,但是它可以工作。

so the code behind becomes: 所以后面的代码变成:

    private void SelectPatientCombobox_TargetUpdated(object sender, DataTransferEventArgs e)
    {
        if ( false == ((ComboBox) sender).IsLoaded) return;
        if ((ComboBox)sender == null) return;
        ((ComboBox) sender).IsDropDownOpen = ((ComboBox) e.Source).Items.Count > 0;
        ((ComboBox)sender).SelectedIndex = -1;

        // when the result is returned, the text is highlighted.
        // typing when text is selected erases the text already typed.
        // so this total hack is a way to press the right arrow key after the results
        // come back so that the text is no longer selected.

        const Key key = Key.Right; // Key to send
        var target = Keyboard.FocusedElement;    // Target element
        var routedEvent = Keyboard.KeyDownEvent; // Event to send
        var inputSource = PresentationSource.FromVisual((ComboBox)sender);

        target.RaiseEvent(
            new KeyEventArgs(
                    Keyboard.PrimaryDevice,
                    inputSource,
                    0,
                    key)
                { RoutedEvent = routedEvent }
        );
    }

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

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