locked
Textbox, text alignment RRS feed

  • Question

  • Hi!

    My problem is regarding the TEXTBOX: allthough you can set the Textalignmnet to right or left it doesn't seem to affect the alignment of the Selected text. When making a text selection the selected text allways seems to be right aligned. Is there any solution to get the selected text to be left aligned?

    The reason i want to do this is that i use the textbox in a custom dropdownlist component.


    k n
    Wednesday, May 18, 2011 1:41 PM

Answers

  • Is it really necessary to have that FindVisualChildByName method, especially when looking for a Template part?  Isn't this more appropriate (and consistent with .NET recommendations):

     

    TextBox tb = sender as TextBox;
    ScrollViewer contentHost = tb.Template.FindName("PART_ContentHost", tb) as ScrollViewer;
    

     

     


    Great, forget the simple way. :)
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 19, 2011 5:58 PM

All replies

  • Hi,

     

    I can't seem to replicate your issue. Is it possible to post some code?

    Wednesday, May 18, 2011 2:18 PM
  • Hi,

    Are you using any styling, or resource dictionary where default style is been added?

    Looks like a style override issue.

    Regards,


    Md. Masudur Rahman
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Sheldon _Xiao Tuesday, May 24, 2011 9:51 AM
    • Unproposed as answer by Sheldon _Xiao Tuesday, May 24, 2011 9:51 AM
    Wednesday, May 18, 2011 2:48 PM
  • Hi kjell nilsson,

    Could you please show some code here you use the TextBox in the component?

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 19, 2011 4:51 AM
  • You don't relly need a custom control to replicate this problem i am having. From your answers i understand i wasn't explaining my problem correctly.

    Let me be a little bit clearer:

     

    Test Case:

    1. Create a textbox

    2. Set the Textbox.text property to a text longer than the physical dimensions of the textbox.

    3. Run the Application.

    4. Select all the text by double click. Now the text will be selected and the end of the text will be shown. Example: (Text: "aaaaaaaaaaaaaaBBBBBBBBBBBBBbBBB")

    the selectedd text shown in the textbox is : "aBBBBBBBBBBBBBbBBB" i want it to be: aaaaaaaaaaaaaaBB.

    I realize that this is probably the proper behaviour you want from this component. But in my case i want the beginning of the text shown instead.

    My question here is: Is there a way to override this behaviour ? I have made an custom controller and tried several ways of changing behaviour without success.

    Thanks in advance.


    k n
    Thursday, May 19, 2011 8:35 AM
  • Hi,

    To achieve that kind of effect I would place a converter on the selecteditems. The converter can then check the string, count the No of characters and e.g. if it is 20 characters it can return the 1st 15 characters.

    The only problem i see with this method is you are going to reduce the and it might not be comprehensible to tyhe user.

     

    Heres a link on how to use a converter (There quite simple) -

    http://blogs.imeta.co.uk/FMoreno/archive/2008/11/07/498.aspx

     

    I hope that helps?

    • Proposed as answer by Pritesh3 Thursday, May 19, 2011 8:41 AM
    Thursday, May 19, 2011 8:40 AM
  • Hi. Sorry. But doesn't that change the contents (text) of the textbox? That is not a valid solution for me since for instance the text i used to make searches in a database.

     

    /Kjell.


    k n
    Thursday, May 19, 2011 9:01 AM
  • Oaky I see your issue -

     

    Bind a public property on your selectedItem. When the value value is set take the value and store it in a private field and then modify the public property to return the reduced string (for display purposes) and then when you eventually do your search use your private variable NOT

     

    e.g.

    private string dbSearchString;

     

    private string selectedText,

    public string SelectedText

    {

    get

    { return selectedText;}

    set {

    dbSearchString = value;

    selectedText = value;

    selectedText = MethodToReturnFirst10Characters();

    }

    does that solve your problem?

    }

    • Proposed as answer by Pritesh3 Thursday, May 19, 2011 9:15 AM
    Thursday, May 19, 2011 9:15 AM
  • sorry my explanation was a little "dodgy" but hopefulyy the little snippet of code made it a little clearer?

    Bind the public SelectedText property to the SelectedItem property of the combobox

    SelectedItem={Binding Path=SelectedText}

     

    if I missed havent explained something clearly give us a shout back.

    Thursday, May 19, 2011 9:18 AM
  • Hi,

    From the default template of the TextBox, it has a ScrollViewer in its template named "PART_ContentHost", so we could get it by VisualTreeHelper and call the ScollToLeftEnd method in the TextBox.SelectionChanged event:

        private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
          if ((sender as TextBox).IsLoaded)
          {
            ScrollViewer scrollviewer = FindVisualChildByName<ScrollViewer>(sender as TextBox, "PART_ContentHost");
            scrollviewer.ScrollToLeftEnd();
          }
        }
    
        private T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
        {
          for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
          {
            var child = VisualTreeHelper.GetChild(parent, i);
            string controlName = child.GetValue(Control.NameProperty) as string;
            if (controlName == name)
            {
              return child as T;
            }
            else
            {
              T result = FindVisualChildByName<T>(child, name);
              if (result != null)
                return result;
            }
          }
          return null;
        }
    
    Sincerely,
    


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jie Bao Thursday, May 19, 2011 3:59 PM
    Thursday, May 19, 2011 3:59 PM
  • Is it really necessary to have that FindVisualChildByName method, especially when looking for a Template part?  Isn't this more appropriate (and consistent with .NET recommendations):

    TextBox tb = sender as TextBox;
    ScrollViewer contentHost = tb.Template.FindName("PART_ContentHost", tb) as ScrollViewer;
    

     

    • Proposed as answer by Jie Bao Thursday, May 19, 2011 5:58 PM
    Thursday, May 19, 2011 5:45 PM
  • Is it really necessary to have that FindVisualChildByName method, especially when looking for a Template part?  Isn't this more appropriate (and consistent with .NET recommendations):

     

    TextBox tb = sender as TextBox;
    ScrollViewer contentHost = tb.Template.FindName("PART_ContentHost", tb) as ScrollViewer;
    

     

     


    Great, forget the simple way. :)
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 19, 2011 5:58 PM
  • This is exactly what i was looking for. Thanks for your answer. Now... i need to do the same thing in Silverlight. Is that a different forum?

     

    /Thanks a lot


    k n
    Friday, May 20, 2011 2:54 PM