How to set the selected text of combobox align left
-
Monday, January 25, 2010 2:04 AMHi all,
In wpf,the selected text of combobox aligns right by default,but my request needs it algins left.I do not find any properties to set to achieve.I also try the FlowDirection property,but the effect is not my want.
Any reply would be appreciated
Thanks
aaron- Edited by aaron yang Monday, January 25, 2010 2:17 AM addition
All Replies
-
Monday, January 25, 2010 5:00 AM
You should be able to use HorizontalContentAlignment="Left".. Hope that helps
Matt Hohn
-
Monday, January 25, 2010 5:50 AM
Thanks for your reply.This property is just for the items of combobox.My request is the selected text of combobox.
aaron -
Monday, January 25, 2010 6:15 AM
hi,
<ComboBox> <ComboBox.ItemContainerStyle> <Style TargetType="{x:Type SelectedItem}" > <Setter Property="HorizontalContentAlignment" Value="Left" /> </Style> </ComboBox.ItemContainerStyle> </ComboBox>
Nagarjuna Dilip -
Monday, January 25, 2010 6:56 AMI am sorry for describing my request unclearly.Actually ,by default combobox aligns selected value left,but when the selected value is
too long for combobox to show it in its textbox,and the selected value will be displayed from right to left,so some left characters will be
hidden by textbox,my request is let combobox displays selected value from left to right in its textbox.By the way,my combobox is
editable
thanks
aaron- Edited by aaron yang Tuesday, January 26, 2010 1:29 AM additional
-
Tuesday, January 26, 2010 7:37 AMModerator
Hi aaron,
I can understand your requirement. The problem is not caused by the alignment property but a feature of TextBox. When you set the IsEditable property to true, ComboBox will apply a template with a TextBox. When you select an item, the text of that item will be filled into the TextBox template and all text will be selected. When all the text is selected and the cursor is at the last index, text “aligns to right”.
To workaround this issue, you can handle the DropDownClosed event to change selected text length.
<ComboBox Name="comboBox1" Width="150" IsEditable="True" DropDownClosed="comboBox1_DropDownClosed">
<ComboBox.Items>
<ComboBoxItem Content="abcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopq"/>
<ComboBoxItem Content="abc"/>
<ComboBoxItem Content="cde"/>
</ComboBox.Items>
</ComboBox>
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
TextBox textBox = (TextBox)GetDescendantByType(this.comboBox1, typeof(TextBox));
textBox.SelectionStart = 0;
textBox.SelectionLength = 0;
}
public static Visual GetDescendantByType(Visual element, Type type)
{
if (element == null) return null;
if (element.GetType() == type) return element;
Visual foundElement = null;
if (element is FrameworkElement)
(element as FrameworkElement).ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type);
if (foundElement != null)
break;
}
return foundElement;
}
Calling GetDescendantByType method can retrieve the TextBox from ComboBox. Then you can set the SelectionStart and SelectionLength to deselect any letters. That avoid the problem.
Hope it helps.
Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- Marked As Answer by aaron yang Wednesday, January 27, 2010 1:31 AM
-
Wednesday, January 27, 2010 1:31 AM
thanks for your answer
aaron -
Wednesday, February 23, 2011 4:41 PMperfect solution match, thx.
-
Friday, March 30, 2012 7:11 PMSet the IsEditable to Flse and you will have the desired behaviour

