Search code examples
c#xamlsilverlighttextboxsilverlight-5.0

Left Text Alignment When Focused and Text is Selected


So here's my problem. On any TextBox or control that includes a TextBox (Like for Example an AutoComplete ComboBox like RadComboBox with IsEditable="True") it's expected behavior that once you select and highlight the text, it aligns to the right so you can see the end.

The issue I'm having can be demonstrated really simply by something like;

<TextBox Text="Blah1 Blah2 Blah3 Blah4 Blah5 Blah6 Blah7 Blah8 Blah9 Blah10" Width="75"/>

visual reference;

enter image description here

So when it's displayed you only see "Blah1 Blah2" but if you select/highlight all the text it automatically goes to a right alignment. Which is fine for most cases, but the problem is like in this case, the user wants to be able to select/highlight the contained text and have it remain left aligned while selected/highlighted and ignore the overflow. (Edit: Not completely ignore the overflow, just not display it highlighted and remain left justified)

I've tried the extent of what I know to do this just short of trying to hunt down whatever the behavior is that does it. Does someone know a handy trick or a some property I've somehow overlooked or something?


Solution

  • Ok so it took me a bit to hunt down the culprit and figure out a fix but this approach works delightfully for the first part of my problem, and the second part.

    All I ended up doing for the first part of my problem (which was an editable combobox always displaying the selectiontextbox's contents either right or center justified) was attach an event trigger in the controltemplate to set the SelectionStart \ SelectionLength to "0" when Loaded. This basically allowed it to be populated and then slap that damn text back over to left alignment and was way less of a pain in my *** than expected.

    So something like (in pseudo);

    <ControlTemplate>
       <TextBox>
          <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                      <ei:ChangePropertyAction TargetName="PART_EditableTextBox"
                                                         PropertyName="SelectionLength"
                                                         Value="0" />
                </i:EventTrigger>
           </i:Interaction.Triggers>
        </TextBox>
    </ControlTemplate>
    

    and more or less the same sort of thing for when text is selected and also setting SelectionStart back to "0", got to love some SL, thanks for looking.