2009-09-17 52 views
10

当我在我自动打开的组合框键入使下拉列表WPF:组合框的下拉highlightes文本

searchComboBox.IsDropDownOpen = true; 

这里的问题是 - 文本被高亮显示,未来keystrock覆盖以前的文本。

如何在ComboBox DropDown打开时禁用文本高亮显示?

+0

其中“组合框下拉”你在说什么? – Trainee4Life 2009-09-18 02:41:43

+3

您是否将false设置为组合框的IsTextSearchEnabled属性?它将禁止选择文本。希望这会有所帮助。 – 2009-09-19 03:02:19

回答

5

比从未更好的迟到,如果其他人打这个proplem他可能会使用它。

如果您覆盖组合框,那么就有这个问题了。 首先获取模板中使用的文本框的句柄并注册到selectedchanged事件。

public override void OnApplyTemplate() 
{ 
    base.OnApplyTemplate(); 

    var element = GetTemplateChild("PART_EditableTextBox"); 
    if (element != null) 
    { 
    var textBox = (TextBox)element; 
    textBox.SelectionChanged += OnDropSelectionChanged; 
    } 
} 

private void OnDropSelectionChanged(object sender, RoutedEventArgs e) 
{ 
    // Your code 
} 

然后在事件处理程序中,您可以再次设置选择,就像您希望的那样。在我的情况下,我在代码中调用IsDropDownOpen。然后保存选择,然后将其放回到事件处理程序中。丑陋,但伎俩。

+0

请你能写出完整的代码我有完全相同的问题,但在这个新的,所以请阐明。我的元素不是一个文本框,而是一个组合框。 – flexxxit 2013-06-22 12:23:51

+0

您可以请在textbox_SelectionChanged中发布示例代码吗?这是这样吗? TextBox tb =(TextBox)e.Source; (tb!= null) { tb.SelectionStart = 0; if(tb!= null) } – 2013-07-11 23:17:19

0

当一个comboxbox获得焦点时,您可以禁用文本高亮显示(即通过在GotFocus事件中不选择文本)。但是,当您拉下组合框时,系统将在列表中找到该项目,并将其作为所选项目。这又会自动突出显示文本。如果我了解你正在寻找的行为,我不相信这是完全可能的。

+0

你是对的?即使我使用IsText提供的IsTextSearchEnabled属性为false,它似乎也不可能。 – Panks 2009-09-21 13:30:10

6

我有这个非常相同的问题,像一些WPF的新用户,努力得到EinarGuðsteinsson给出的解决方案。所以为了支持他的回答,我在这里粘贴了让这个工作起来的步骤。 (或者更准确地说,我是如何得到这个工作的)。

首先创建一个继承自Combobox类的自定义组合框类。请参阅下面的代码以获取完整实现您可以更改OnDropSelectionChanged中的代码以满足您的个人需求。

namespace MyCustomComboBoxApp { using System.Windows.Controls;

public class MyCustomComboBox : ComboBox 
{ 
    private int caretPosition; 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     var element = GetTemplateChild("PART_EditableTextBox"); 
     if (element != null) 
     { 
      var textBox = (TextBox)element; 
      textBox.SelectionChanged += OnDropSelectionChanged; 
     } 
    } 

    private void OnDropSelectionChanged(object sender, System.Windows.RoutedEventArgs e) 
    { 
     TextBox txt = (TextBox)sender; 

     if (base.IsDropDownOpen && txt.SelectionLength > 0) 
     { 
      txt.CaretIndex = caretPosition; 
     } 
     if (txt.SelectionLength == 0 && txt.CaretIndex != 0) 
     { 
      caretPosition = txt.CaretIndex; 
     } 
    } 

} 

确保此自定义组合类存在于同一个项目中。您可以使用下面的代码在您的用户界面中引用此组合。

<Window x:Class="MyCustomComboBoxApp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:cc="clr-namespace:MyCustomComboBoxApp" 
    Title="MainWindow" Height="350" Width="525" FocusManager.FocusedElement="{Binding ElementName=cb}"> 
<Grid> 
    <StackPanel Orientation="Vertical"> 
     <cc:MyCustomComboBox x:Name="cb" IsEditable="True" Height="20" Margin="10" IsTextSearchEnabled="False" KeyUp="cb_KeyUp"> 
      <ComboBoxItem>Toyota</ComboBoxItem> 
      <ComboBoxItem>Honda</ComboBoxItem> 
      <ComboBoxItem>Suzuki</ComboBoxItem> 
      <ComboBoxItem>Vauxhall</ComboBoxItem> 
     </cc:MyCustomComboBox> 
    </StackPanel> 
</Grid> 
</Window> 

那就是它!有任何疑问,请询问!我会尽力帮助。

谢谢他的解决方案EinarGuðsteinsson!

3

我认为在由Andrew N提供的解决方案中存在一些缺失,因为当我尝试使用TextBox的Selection Changed事件时,将脱字符号放置在错误的位置。所以我做了这个改变来解决这个问题。

namespace MyCustomComboBoxApp { using System.Windows.Controls; 

public class MyCustomComboBox : ComboBox 
{ 
    private int caretPosition; 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     var element = GetTemplateChild("PART_EditableTextBox"); 
     if (element != null) 
     { 
      var textBox = (TextBox)element; 
      textBox.SelectionChanged += OnDropSelectionChanged; 
     } 
    } 

    private void OnDropSelectionChanged(object sender, System.Windows.RoutedEventArgs e) 
    { 
     TextBox txt = (TextBox)sender; 

     if (base.IsDropDownOpen && txt.SelectionLength > 0) 
     { 
      caretPosition = txt.SelectionLength; // caretPosition must be set to TextBox's SelectionLength 
      txt.CaretIndex = caretPosition; 
     } 
     if (txt.SelectionLength == 0 && txt.CaretIndex != 0) 
     { 
      caretPosition = txt.CaretIndex; 
     } 
    } 
} 
+0

伟大的现货和修复穆罕默德! – 2017-08-08 08:20:17

2

继clsturgeon的回答,我已经解决了通过设置选择的问题,当DropDownOpened事件发生:

private void ItemCBox_DropDownOpened(object sender, EventArgs e) 
{ 
    TextBox textBox = (TextBox)((ComboBox)sender).Template.FindName("PART_EditableTextBox", (ComboBox)sender); 
    textBox.SelectionStart = ((ComboBox)sender).Text.Length; 
    textBox.SelectionLength = 0; 
} 
+1

它的工作原理,我找到了最短的解决方案,谢谢你:D – Kreshnik 2017-01-28 16:56:38

+0

现在来看看如何使它成为一个DependencyProperty ... – KornMuffin 2017-08-18 21:33:50