简体   繁体   English

如何在代码中将Selector.IsSelected附加属性设置为ListBox项?

[英]How do I set the Selector.IsSelected attached property to ListBox items in code?

Reference to WPF 4 Unleashed page 281: "Selector also supports two attached properties that can be applied to individual items". 请参考WPF 4 Unleashed页面281:“选择器还支持两个可以应用于单个项目的附加属性”。

<ListBox Name="listBox1">
    <sys:String>Fred</sys:String>
    <sys:String Selector.IsSelected="True">Wilma</sys:String>   <- Error
    <ListBoxItem Selector.IsSelected="True">
        <sys:String>Barney</sys:String>
    </ListBoxItem>
</ListBox>

Seems I can't apply Selector.IsSelected to the second item because it isn't a DependencyObject is that right? 似乎我不能将Selector.IsSelected应用于第二个项目,因为它不是DependencyObject是吗? Can't do it from code either. 也不能通过代码来完成。

Another question, if I want to add strings to the Items collection in XAML is there a way to separate them without wrapping them inside <sys:String></sys:String> ? 另一个问题,如果我想将字符串添加到XAML中的Items集合中,有没有一种方法可以将它们分开而不将它们包装在<sys:String></sys:String>

What can I say 我能说什么

1) Yes you're correct you can only attach an attached property to a DependencyObject . 1)是的,您是正确的,您只能将附加属性附加到DependencyObject An alternative would be to use <ListBox SelectedIndex="1"> ... 一种替代方法是使用<ListBox SelectedIndex="1"> ...

2) You have to wrap the strings in something, but it could be: 2)您必须将字符串包装在某些东西中,但是可能是:

<sys.String>Fred</sys.String>
<ListBoxItem>Fred</ListBoxItem>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM