Listbox wpf中选择的TextBlocks值 [英] Selected TextBlocks value in Listbox wpf
本文介绍了Listbox wpf中选择的TextBlocks值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表框和一个包含网格和两个文本块(ID,描述)的模板。我使用此模板来查看访问数据库上的某些查询的结果。我已将文本块绑定到数据表列。
但是,如何才能获得所选的描述文本块的值,如字符串?
I have a listbox and a template that contains a grid and two textblocks(ID , Description). I have used this template to view the results of some query on the access database. I have binded the textblocks to columns of datatable.
However, how can i get the value of just the description textblock which is selected, as string?
推荐答案
如果列表框是多选的,您可以使用属性SelectedItems
,否则您可以使用属性SelectedItem
,SelectedValue
或SelectedIndex
。你会来到项目引用,它实际上可以是任何类型,而不仅仅是字符串,所以返回对象的编译时类型是System.Object
;您可以检查其运行时类型,但从您的设计中您知道它是什么。它应该是TextBlock
或带有子文本块的一些UI元素(我不知道你的确切设计,因为你没有共享任何代码,但你应该知道它) 。这样,您来到TextBlock
的实例,因此您可以使用属性TextBlock.Text
提取文本。这就是全部。
请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx [ ^ ],
http://msdn.microsoft.com/en-us/ library / system.windows.controls.primitives.selector.selectedindex.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selecteditem.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/ system.windows.controls.listbox.selecteditems.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvalue.aspx [ ^ ],
http://msdn.microsoft.com/ EN-US /李brary / system.windows.controls.textblock.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.text.aspx [ ^ ]。
-SA
If the list box is multi-selectable, you can use the propertySelectedItems
, otherwise you can use the propertySelectedItem
,SelectedValue
orSelectedIndex
. You will come to the item reference, which can actually be of any type, not just string, so the compile-time type of the returned object isSystem.Object
; you can check up its runtime type, but from your design your know what is it. It should be eitherTextBlock
or some UI elements with to child text blocks (I don't know you exact design as you did not share any code, but you should know it). This way, you come to the instance ofTextBlock
, so you can extract the text using the propertyTextBlock.Text
. That's all.
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedindex.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selecteditem.aspx[^]
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selecteditems.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvalue.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.text.aspx[^].
—SA
这篇关于Listbox wpf中选择的TextBlocks值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文