且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式选择列表框中的项目/索引

更新时间:2023-11-28 21:38:10

您可以这样做的一种方法是将 Selected 字段添加到您的数据对象.然后,您需要覆盖默认的 listboxitem 样式并将 isselected 属性绑定到对象中的 Selected 属性.然后你只需要检查你的数据项并更新 Selected 值.

One way you can do this is to add a Selected field to your data object. Then you need to overide the default listboxitem style and bind the isselected property to the Selected property in your object. Then you just need to go through your data items and update the Selected value.

如果您不将该 Selected 属性实现为依赖属性,则需要您的类实现 INotifyPropertyChanged 接口并在设置值时引发 propertychanged 事件.

If you don't implement that Selected property as a dependency property, you need your class to implented the INotifyPropertyChanged interface and raise the propertychanged event when you set the value.