【问题标题】:WPF ListView IsSelected Set Border OpacityWPF ListView IsSelected 设置边框不透明度
【发布时间】:2017-04-16 00:37:40
【问题描述】:

我有一个列表视图,填充必须为 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>

看起来像这样;

并且被选中;

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

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

我该怎么做?感谢您的建议。

【问题讨论】:

    标签: wpf listview


    【解决方案1】:

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

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

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

    【讨论】:

    • 我知道但只是想知道如何应用到边缘。
    • 抱歉,您的问题很难理解。您应该将目标命名为“edge”并在触发器中定位它。此外,如果您尝试使用 Blend,它可能会更容易玩。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2012-01-24
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多