Visual Studio C#中wpf应用程序中的ListBox处理 [英] ListBox handling in wpf application in visual studio C#

查看:76
本文介绍了Visual Studio C#中wpf应用程序中的ListBox处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释一下这段代码





ListBoxItem lbi =(listbox1.SelectedItem as ListBoxItem);

tb1.Text = lbi.Content.ToString();

can any one please explain me this code


ListBoxItem lbi = (listbox1.SelectedItem as ListBoxItem);
tb1.Text = lbi.Content.ToString();

推荐答案

WPF ListBox与WinForms ListBox一样,存储了一组对象,它们是通过'Items属性访问,或者,如此处,通过访问'SelectedItem属性。



因此,'SelectedItem 返回一个对象:然后,必要将该Object转换回ListBoxItem,因此可以访问其内部字段。转换是在这里使用'as运算符完成的。



ListBoxItem的'Content字段在技术上是ContentControl依赖属性:[ ^ ]。



在这种情况下,看起来程序员预计内容字段包含字符串,并且需要使用'ToString()来获取字符串值。
The WPF ListBox, like the WinForms ListBox, stores a collection of Objects, which are accessed through its 'Items property, or, as in the case here, via accesing the 'SelectedItem property.

So, 'SelectedItem returns an Object: then, it's necessary to convert that Object back into a ListBoxItem, so its internal fields can be accessed. The conversion is accomplished here using the 'as operator.

The 'Content field of a ListBoxItem is technically a ContentControl dependency property: [^].

In this case, it looks like the programmer anticipated that the 'Content field did not contain a string, and required the use of 'ToString() to get a string value.


这篇关于Visual Studio C#中wpf应用程序中的ListBox处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆