WPF ListView IsSelected设置边框不透明度

时间:2017-04-16 00:37:40

标签: wpf listview

我有一个列表视图,填充必须是2并且颜色获得系统主题(使用Windows 8.1)但看起来非常难看。

<ListView.Resources>
<Style TargetType="ListViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Border Name="Border" Padding="2" SnapsToDevicePixels="True">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{x:Static SystemParameters.WindowGlassBrush}"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="IsSelected" Value="true"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ListView.Resources>

提示:我在DataTemplate中使用了CornerRadius = 10.

<DataTemplate x:Key="DT">
<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="50*"/>
        </Grid.RowDefinitions>

        <Border Background="{Binding HEXRengi}" MinHeight="100" MinWidth="150" Height="100" Width="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="2" CornerRadius="10"/>


        <ToggleButton x:Name="HEXRenkAl" Style="{DynamicResource DüzRenkPaletiDüğmeBiçimi}" Grid.Row="0" Click="HEXRenkAl_Click">
            <Border Background="{Binding HEXRengi}" MinHeight="50" MinWidth="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="10">
                <TextBox Text="{Binding HEXRengi}" Style="{DynamicResource SeçilebilirAmaYazılamazMetinKutusu}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Justify" TextWrapping="Wrap" Foreground="White" FontFamily="Century Gothic" FontSize="16" FontWeight="Bold"/>
            </Border>
        </ToggleButton>

        <ToggleButton x:Name="RGBRengiAl" Style="{DynamicResource DüzRenkPaletiDüğmeBiçimi}" Grid.Row="1" Click="RGBRengiAl_Click">
            <Border Background="{Binding HEXRengi}" MinHeight="50" MinWidth="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="05">
                <TextBox Text="{Binding RGBRengi}" Style="{DynamicResource SeçilebilirAmaYazılamazMetinKutusu}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Justify" TextWrapping="Wrap" Foreground="White" FontFamily="Century Gothic" FontSize="16" FontWeight="Bold"/>
            </Border>
        </ToggleButton>

    </Grid>
</StackPanel>
</DataTemplate>

看起来像这样;

enter image description here

Is Islected;

enter image description here

当我使用setter属性不透明度值时,应用所有项目。 应该在边框上申请,而不是所有项目

<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{x:Static SystemParameters.WindowGlassBrush}"/>
<Setter TargetName="Border" Property="Opacity" Value=".5"/>
</Trigger>

enter image description here

我该怎么办?谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

  

当我使用setter属性不透明度值时,应用所有项目。只应该申请边框,而不是所有项目。

如果您想知道为什么不透明度值适用于所有项目,那是因为您已命名为“边框”,并且您在Trigger中按名称定位。

<Border Name="Border" Padding="2" SnapsToDevicePixels="True">
    <ContentPresenter />
 </Border>