[英]Get Highlighted Item From Editable Combobox in WPF
I have Editable Combobx where I am adding Strings dynamically based on some logic instead of Combobox Items. 我有可编辑的Combobx,我在其中基于某种逻辑而不是组合框项目来动态添加字符串。
Now, I want to get Highlighted Item when combobox get focused, I know Combobox Item has IsHighlighted Property which can work but since I am adding as a string, 现在,我想在组合框获得焦点时获得突出显示的项目,我知道组合框项目具有IsHighlighted属性,该属性可以工作,但是由于我是作为字符串添加的,
How Can I get Highlighted Sting from C# ? 如何从C#中获得突出显示的毒刺? note that if I am trying to convert string to Combobox Item its erroring out! 请注意,如果我尝试将字符串转换为Combobox Item,则会出错!
不确定我是否真的了解您要做什么,但是获取与所选项目关联的ComboBoxItem的一种方法是执行以下操作:
ComboBoxItem comboBoxItem = (ComboBoxItem)DraggedComboBox2.ItemContainerGenerator.ContainerFromItem(DraggedComboBox2.SelectedItem);
the code you have wrote get the selected item not the highlighted one !! 您编写的代码将获得所选项目,而不是突出显示的项目! this example will do the job for you : 这个例子将为您完成工作:
note : in the code behind file you use the event gotfocus for the highlighted and lostfocus for to know when an item become "unhighlighted": 注意:在文件后面的代码中,将事件gotfocus用于突出显示和丢失焦点,以了解项目何时变为“未突出显示”:
<ComboBox Height="33" HorizontalAlignment="Left" Margin="128,107,0,0" Name="comboBox1" VerticalAlignment="Top" Width="245">
<ComboBox.Resources>
<Style TargetType="ComboBoxItem">
<EventSetter Event="GotFocus" Handler="GotFocusHandler" />
<EventSetter Event="LostFocus" Handler="LostFocusHandler" />
</Style>
</ComboBox.Resources>
<ComboBoxItem Content="Cat 1" />
<ComboBoxItem Content="Cat 2" />
<ComboBoxItem Content="Cat 3" />
<ComboBoxItem Content="Cat 4" />
</ComboBox>
private void GotFocusHandler(object sender, RoutedEventArgs e)
{
string HighlightedText = (sender as ComboBoxItem).Content.ToString();
//do some thing
}
private void LostFocusHandler(object sender, RoutedEventArgs e)
{
string HighlightedText = (sender as ComboBoxItem).Content.ToString();
//do some thing
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.