【问题标题】:How to make style in ListView SelectedItem.?如何在 ListView SelectedItem 中制作样式。?
【发布时间】:2011-11-16 11:27:59
【问题描述】:

我正在开发一个 WPF 应用程序,我想在 ListViewItem 上创建样式。我为此制定了一种风格,但这并没有给我想要的确切结果。除此之外,我想要这种风格的CornerRadius,那么我该如何添加呢?

            <ListView x:Name="MenuBarList" 
                    Grid.Row="1"
                    Height="{Binding MainMenuHeight}"  
                    Width="{Binding MainMenuWidth}" 
                    ItemsSource="{Binding Path=Menu.Options}" 
                    SelectedItem="{Binding Path=SelectedMainMenuOption, Mode=TwoWay}"
                    Foreground="White"
                    Background="Transparent" 
                    VerticalContentAlignment="Center"
                    HorizontalContentAlignment="Center"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    IsSynchronizedWithCurrentItem="True"
                    IsTabStop="False">

                <ListView.Style>
                    <Style TargetType="{x:Type ListView}">
                        <Setter Property="BorderBrush" Value="White"/>
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="Margin" Value="0"/> 
                        <Setter Property="Width" Value="300"/>
                        <Style.Resources>
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#9449b0"/>
                            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#9449b0"/>
                    </Style.Resources>
                    </Style>
                </ListView.Style>


                <ListView.ItemTemplate>
                    <DataTemplate>

                        <StackPanel Orientation="Horizontal" Focusable="False" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <Image Source="{Binding IconPath}" 
                                   Focusable="False"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   IsHitTestVisible="False"/>

                            <TextBlock Text="{Binding Title}" 
                                       Focusable="False" 
                                       HorizontalAlignment="Center" 
                                       VerticalAlignment="Center" 
                                       FontFamily="Segoe UI"
                                       FontSize="26"
                                       IsHitTestVisible="False"/>
                        </StackPanel>
                </DataTemplate>

                </ListView.ItemTemplate>
            </ListView>

【问题讨论】:

    标签: wpf listview styles


    【解决方案1】:

    快速解决方案是覆盖项目模板。您可以参考 WPF 或 Silverlight 主题来了解 contols 样式。有关 ListViewItem 完整模板,请参阅BureauBlue.xaml

    <ListView x:Name="uiList">
        <ListView.Resources>
            <ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem">
                <Border SnapsToDevicePixels="true" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}"
                        CornerRadius="5" x:Name="border">
                    <ContentControl 
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                        Margin="2,2,2,2" 
                        VerticalAlignment="Stretch"
                        Content="{TemplateBinding Content}" />
                </Border>
            </ControlTemplate>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true" />
                            <Condition Property="Selector.IsSelectionActive" Value="true" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="Pink" />
                        <Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
                    </MultiTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Resources>
        <ListViewItem Content="10" />
        <ListViewItem Content="11" />
    </ListView>
    

    【讨论】:

    • 我想要的结果是我想要 ot SeletedItem 将用粉红色突出显示,因此需要cornerRadius。
    • 我已经把我的代码放到了 XAML 中,所以你可以看一下,我只想要 selectedItem 的圆角。
    • 我必须将 GridViewRowPresenter 添加到 ContentControl 中,以便 ListViewItem 将显示绑定到它的实际项目,否则它将显示绑定到 ListView 的 ItemsSource 的类的名称。顺便说一句,BureauBlue.xaml 链接已失效,您能更新一下吗?
    • @witoong623 我已经更新了与关闭等效项的链接。关于您的障碍, ContentControl 没有定义要显示的内容。您的 ItemTemplate 和绑定定义了如何显示您已绑定到列表的项目。因此,如果您直接绑定文本或使用 ItemTemplate 来更具体地显示您想要显示的项目属性,则不需要 GridViewRowPresenter。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多