5

I am working with editable combobox where text can be entered in the textbox area of WPF combobox. When the length of text entered is greater than the width of combobox, the cursor still shows outside the combobox and on the form but text is not shown. Is there anyway to restrict the cursor from not moving out of the combobox?

Thanks.

user296623
  • 325
  • 3
  • 7
  • 18

3 Answers3

2

Use ScrollViewer for PART_ContentHost instead of border. For example:

<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
    <ScrollViewer x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding  Background}" />
</ControlTemplate>
... 
<TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" 
...
nicolas2008
  • 945
  • 9
  • 11
0

I don't know if you have found a solution to this, but I have the same problem and as I see now it comes from this part of the code:

<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
    <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
</ControlTemplate>

...and inside the ComboBox Style, in the Setter for Template:

... 
<TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" 
...

Removing this template seems to solve the problem and create some other minor problems.

NoOne
  • 3,851
  • 1
  • 40
  • 47
0

You could override the default style and set the MaxLength property on the TextBox being used for content presentation (PART_EditableTextBox) and then reuse that style where needed.

If you would prefer to do it in code you can check out this SO answer which will achieve the same behavior but not force you to create a style.

EDIT:

If you want this to be more dynamic you will have to measure the text. You could handle the TextChanged event and perform the measurement.

Community
  • 1
  • 1
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • Hi Aaron, Thanks for the reply. I set MaxLength which is the number of characters to be entered into textarea of combobox. However, the problem here is each character occupies different amount of space. Lets say the MaxLength is set to 30 in xaml, When I type in lowercase maybe its good and within the combobox but if I type in uppercase it still exceeds the combobox. Also different combination of characters occupies different space. – user296623 Dec 29 '10 at 15:37
  • This is my idea: If I can get the position at which cursor reaches the edge of the textbox in combobox control(just like in Text wrapping), I can set this value to MaxLength instead of hardcoding in xaml. But as of now, not sure how to do this. – user296623 Dec 29 '10 at 15:41
  • @user296623 There are caveats of course with this approach. If you can allow your ComboBox to be as wide as possible then this becomes a non-issue. Not sure if you have to deal with localization either as that could prove to be another interesting area. You can also make the editable portion of the ComboBox grow...so its width is not fixed which would allow the ComboBox to grow as typing took place. It all boils down to screen real estate though....not sure what your root issue is. – Aaron McIver Dec 29 '10 at 15:41
  • @user296623 You would have to measure the text, added additional commentary in post – Aaron McIver Dec 29 '10 at 15:43