【问题标题】:Listbox style selection does not work with datatemplate列表框样式选择不适用于数据模板
【发布时间】:2012-05-17 21:07:54
【问题描述】:

我在这里使用 ListBox 和 ListBox 样式:http://viblend.com/products/net/wpf/controls/free-wpf-controls.aspx

看起来像这样:

    <ListBox x:Name="listBox" ItemsSource="{Binding}" Style="{StaticResource Office2010SilverListBoxStyle}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ListBoxItem Style="{StaticResource Office2010SilverListBoxItemStyle}" Content="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

但是,如果我单击一个项目,它不会被标记为选定项目。 如果我不使用数据模板,一切正常。那么有人知道为什么吗? 这里是工作示例:

<ListBox x:Name="listBox" Style="{StaticResource Office2010SilverListBoxStyle}">
    <ListBoxItem Style="{StaticResource Office2010SilverListBoxItemStyle}">1. Frame</ListBoxItem>
    <ListBoxItem Style="{StaticResource Office2010SilverListBoxItemStyle}">2. Frame</ListBoxItem>
    <ListBoxItem Style="{StaticResource Office2010SilverListBoxItemStyle}">3. Frame</ListBoxItem>
    <ListBoxItem Style="{StaticResource Office2010SilverListBoxItemStyle}">4. Frame</ListBoxItem>
    <ListBoxItem Style="{StaticResource Office2010SilverListBoxItemStyle}">5. Frame</ListBoxItem>
    <ListBoxItem Style="{StaticResource Office2010SilverListBoxItemStyle}">6. Frame</ListBoxItem>
</ListBox>-->

【问题讨论】:

    标签: wpf listbox


    【解决方案1】:

    您的DataTemplate 应该不包含ListBoxItem,它将由ListBox 控件创建,在您的示例中,ListBoxItem 中将有一个ListBoxItem,混淆选择。

    您还应该通过ItemContainerStyle 将样式应用于项目。 (据我所知,这仅适用于自动生成的ListBoxItems,如果您直接或通过ItemsSource 添加数据,如果您直接添加ListBoxItems,DataTemplate 和ItemContainerStyle 都将被忽略)

    另请参阅:Data Templating Overview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 2011-11-08
      • 1970-01-01
      • 2020-05-21
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多