【问题标题】:WPF ListBox apply general Style to ListBoxItemsWPF ListBox 将一般样式应用于 ListBoxItems
【发布时间】:2012-05-15 14:07:47
【问题描述】:

我是否可以在列表框声明中说所有项目都具有该样式? 目前我必须告诉每一个项目。

【问题讨论】:

    标签: wpf listbox


    【解决方案1】:

    你可以设置ItemTemplate:

    <Window.Resources>
        <Style x:Key="SomeControlStyle" TargetType="SomeControl" >
            <Setter Property="BorderThickness" Value="2"/>
        </Style>
    
        <DataTemplate x:Key="ADataTemplate">
            <SomeControl Style="{StaticResource SomeControlStyle}"/>
        </DataTemplate>
    </Window.Resources>
    
    <ListBox ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <SomeControl Style="{StaticResource SomeControlStyle}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    或者:

    <ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource ADataTemplate}"/>
    

    【讨论】:

    • 所以如果我是对的,这应该有效吗? ListBox.ItemTemplate> 但它只工作“一半”。问题是我可以单击项目并且样式有效,但未选择。我使用办公室风格,所以只有鼠标悬停在黄色上单击并选择没有任何反应
    • 你怎么知道项目没有被选中?请将您的评论中的信息添加到您的问题中,这将使问题变得更好、更容易回答。
    • 如果它没有被选中,我不知道确切,但我的风格没有将它标记为选中。如果我正在做类似的事情:1。框架它工作
    • 如果 ListBox 的 SelectedItem 属性在单击项目后设置,则仅缺少视觉线索。你应该测试一下。
    猜你喜欢
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2010-11-30
    • 2018-09-23
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多