Getting Started with Adobe After Effects - Part 6: Motion Blur

Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 119
#SQLServer 88
Oracle 70
BRH 31
SQL Server 2012 29
denali 23
#TSQL 19
C# 15

Archive · View All
October 2011 31
November 2011 30
September 2011 30
August 2011 18
December 2011 15
July 2011 13
June 2011 8
May 2012 4
April 2012 3
January 2010 3

Get Selected/Highlighted Text from WPF ComboBox

Apr 30 2012 10:38PM by Niladri Biswas   

Hi, I a recent requirement of mine, I had to choose only the text that has been highlighted/selected in a WPF combo box which is editable.


Let us say we have a WPF combobox and a button as given below

<ComboBox  Name="comboBox1" IsEditable="True" />
<Button Content="Get Selected Text" Name="button1" Click="button1_Click" />

Now, we have written some text in the combobox at runtime say

DLF/ILP 2012 is rocking at it's best.

After that by using mouse/keyboard, say we have highlighted "rocking".

Now when we click the button the result will be "rocking".

For doing so, I have written the below code

private void button1_Click(object sender, RoutedEventArgs e)
	string result = string.Empty;
	var editableTextBox = comboBox1.Template.FindName("PART_EditableTextBox", comboBox1) as TextBox;
	if (editableTextBox != null)
		result = editableTextBox.SelectedText;

	MessageBox.Show(string.Format("Selected Text is {0}",result));


Code Explanation

The ComboBox doesn't track the text selection itself, so if we want to get the selected text we will need to find the TextBox in the template for the ComboBox and read the selection information from there.The ComboBox class has a TemplatePartAttribute:[TemplatePartAttribute(Name = "PART_EditableTextBox", Type = typeof(TextBox))]. This specifies the control template expects a part with that name and that type.

Hope this will be helpful.

Tags: WPF,

Niladri Biswas
7 · 21% · 6720


Your Comment

Sign Up or Login to post a comment.

"Get Selected/Highlighted Text from WPF ComboBox" rated 5 out of 5 by 4 readers
Get Selected/Highlighted Text from WPF ComboBox , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]