2009-09-17 97 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