从WPF ListView中的行中删除玻璃效果

时间:2011-09-06 12:18:26

标签: wpf xaml datagrid

我正在尝试在基于WPF的应用程序中设置ListView的样式。我正在尝试做的是在行上的鼠标悬停上创建渐变效果。但我不想要的是添加到行中的标准玻璃效果。即使有我自己的造型(下图),玻璃效果仍然存在。

XAML风格

<Style TargetType="{x:Type ListViewItem}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush>
                                    <GradientStop Color="#39B4E7" Offset="0"/>
                                    <GradientStop Color="#4297BB" Offset="1"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>

的ListView

<ListView Grid.Row="0" ItemsSource="{Binding Projects}" SelectedItem="{Binding CurrentProject}" Margin="3" ItemContainerStyle="{StaticResource ListViewItemContainerStyle}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="" DisplayMemberBinding="{Binding ShortName}" />
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Owner" DisplayMemberBinding="{Binding User.Name}" />
                    <GridViewColumn Header="Versions" DisplayMemberBinding="{Binding Versions.Count}" />
                </GridView>
            </ListView.View>
        </ListView>

新的XAML样式

<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type ListViewItem}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush>
                                    <GradientStop Color="#39B4E7" Offset="0"/>
                                    <GradientStop Color="#4297BB" Offset="1"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>

这可能吗?

1 个答案:

答案 0 :(得分:1)

每个ListViewItem都位于contianer中,而该容器是选择/悬停的背景。因此,您必须为ItemContainerStyle上的ListView属性设置值。

...示例

<ListView ItemContainerStyle="{StaticResource ListViewItemContainerStyle}" />

风格..

<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type ListViewItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
    </Style.Resources>
</Style>

修改 因此,对于您的情况,您可以保留所有内容,只需在ResourceDictionary中添加该样式,并在ItemContainerStyle上添加ListView属性即可。希望这有帮助,如果你遇到困难,请告诉我。

这是我以前做过的事情,但它最终变得乏味,所以我扩展了ListView控件以自动处理这个并添加了悬停/选择颜色的属性。

更新代码:

<ListView Grid.Row="0" ItemsSource="{Binding Projects}" SelectedItem="{Binding CurrentProject}" Margin="3" ItemContainerStyle="{StaticResource ListViewItemContainerStyle}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="" DisplayMemberBinding="{Binding ShortName}" />
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Owner" DisplayMemberBinding="{Binding User.Name}" />
                <GridViewColumn Header="Versions" DisplayMemberBinding="{Binding Versions.Count}" />
            </GridView>
        </ListView.View>
    </ListView>

**资源文件**

<Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush>
                                <GradientStop Color="#39B4E7" Offset="0"/>
                                <GradientStop Color="#4297BB" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>

<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type ListViewItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
    </Style.Resources>
</Style>