【问题标题】:Styling `ListView` menu with round corners用圆角设计“ListView”菜单
【发布时间】:2012-01-26 13:48:17
【问题描述】:

我正在寻找使用ListView 构建菜单(我愿意使用任何其他真正可以达到我正在寻找的效果的东西。我想模仿的外观如下。

这是我目前用于 XAML 的内容。

    <ListView 
        ItemsSource="{Binding Decisions}" 
        SelectedItem="{Binding SelectedDecision}" 
        HorizontalAlignment="Left"
        BorderThickness="0"
        Width="350"
        FontSize="12">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel 
                    Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border 
                    BorderThickness="1" 
                    BorderBrush="#DDD" 
                    CornerRadius="2">
                    <TextBlock 
                        Text="{Binding Converter={StaticResource DecisionTypeNameConverter}}"
                        Padding="8"
                        Background="#EEE" />
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

这会产生一个看起来不错的菜单,直到我选择一个项目。一旦选择了一个项目,它就会被难看的深蓝色包围。如何在 XAML 中重新创建这种外观(不需要渐变效果)?

【问题讨论】:

    标签: wpf xaml listview styling listviewitem


    【解决方案1】:

    您需要跟踪SelectedItem吗?

    如果没有,我建议使用ItemsControl

    如果是这样,您需要覆盖所选项目的画笔颜色,使其不显示。这是一个系统颜色,我通常这样覆盖它:

    <ListView.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    </ListView>
    

    【讨论】:

    • 我确实需要SelectedItem 并且修改画笔效果很好。这有点放在一边,但是一旦选择了项目,我如何为我的背景设置样式(例如)?当IsSelectedTrueListViewItem 时,我有一个触发器,我想更改Border 元素的背景颜色。在我的触发器中将属性设置为 Border.Background 并没有成功。
    • @Nosila 在您的&lt;Border&gt; 标签中,尝试设置Background="{TemplateBinding Background}"。然后您可以在ListViewItem 样式上设置样式触发器,该样式根据IsSelected 是否为真设置背景。
    • 太棒了。非常感谢你。 :D
    【解决方案2】:

    为了防止蓝色选择​​矩形,您必须设置 ItemContainerStyle 而不是 ItemTemplate。请参阅 ListView Styles and Templates 上的 MSDN,了解 ListViewItem 默认样式,尤其是“已选择”视觉状态。

    【讨论】:

    • 谢谢。让我研究一下。
    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多