如何在鼠标结束并选中时更改ListView边框

时间:2015-06-07 13:00:46

标签: wpf xaml listview

在我做了一些更改之后,这是我的ListView项:

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border
                 BorderBrush="White"
                 BorderThickness="0"
                 Background="{TemplateBinding Background}">
                            <GridViewRowPresenter HorizontalAlignment="Stretch"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                              Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>

Background colorTransparentForeground colorWhite

虽然MouseOverListView item selected没有变化,但这意味着当前视图没有变化,我想将BorderColor中的MouseOver更改为WhiteSelected我希望将BorderColor更改为蓝色。

编辑:

尝试代码示例后,这两行:

<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/

收到错误,无法解析资源Item.SelectedActive.Bachground,无法解析资源Item.SelectedActive.Border。

1 个答案:

答案 0 :(得分:0)

要更改MouseOver和所选颜色,请为ListViewItem添加触发器:

<Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Padding" Value="4,1"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                      <Border
             BorderBrush="White"
             BorderThickness="0"
             Background="{TemplateBinding Background}">
                        <GridViewRowPresenter HorizontalAlignment="Stretch"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                    </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="Transparent"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="White"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="False"/>
                                    <Condition Property="IsSelected" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="Transparent"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="Blue"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="True"/>
                                    <Condition Property="IsSelected" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="Transparent"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="Blue"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>