如何更改ListBox中未选定项目的边框?

时间:2014-04-25 20:59:12

标签: xaml windows-runtime winrt-xaml

当控件首次加载时,它看起来也是我想要的方式,但是当选择一个项目时,其他项目周围会出现白色边框。如何使这个白色边框透明?

<ListBox Background="Transparent" ItemTemplate="{StaticResource ServiceDeliveryPlanTemplate}" ItemsSource="{Binding Sdps}" Margin="40,0,40,0" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

1 个答案:

答案 0 :(得分:0)

ListBox模板具有焦点状态的可视状态,用于更改背景:

<VisualStateGroup
    x:Name="FocusStates">
    <VisualState
        x:Name="Focused">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames
                Storyboard.TargetProperty="Background"
                Storyboard.TargetName="LayoutRoot">
                <DiscreteObjectKeyFrame
                    KeyTime="0"
                    Value="{ThemeResource ListBoxFocusBackgroundThemeBrush}" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    <VisualState
        x:Name="Unfocused" />
</VisualStateGroup>

防止这种情况的唯一方法是更改​​该可视状态故事板。以下是默认ListBox样式的完整模板:

<Style
    x:Key="ListBoxStyle1"
    TargetType="ListBox">
    <Setter
        Property="Foreground"
        Value="{ThemeResource ListBoxForegroundThemeBrush}" />
    <Setter
        Property="Background"
        Value="{ThemeResource ListBoxBackgroundThemeBrush}" />
    <Setter
        Property="BorderBrush"
        Value="{ThemeResource ListBoxBorderThemeBrush}" />
    <Setter
        Property="BorderThickness"
        Value="{ThemeResource ListBoxBorderThemeThickness}" />
    <Setter
        Property="ScrollViewer.HorizontalScrollBarVisibility"
        Value="Disabled" />
    <Setter
        Property="ScrollViewer.VerticalScrollBarVisibility"
        Value="Auto" />
    <Setter
        Property="ScrollViewer.HorizontalScrollMode"
        Value="Disabled" />
    <Setter
        Property="ScrollViewer.IsHorizontalRailEnabled"
        Value="True" />
    <Setter
        Property="ScrollViewer.VerticalScrollMode"
        Value="Enabled" />
    <Setter
        Property="ScrollViewer.IsVerticalRailEnabled"
        Value="True" />
    <Setter
        Property="ScrollViewer.ZoomMode"
        Value="Disabled" />
    <Setter
        Property="ScrollViewer.IsDeferredScrollingEnabled"
        Value="False" />
    <Setter
        Property="ScrollViewer.BringIntoViewOnFocusChange"
        Value="True" />
    <Setter
        Property="IsTabStop"
        Value="False" />
    <Setter
        Property="TabNavigation"
        Value="Once" />
    <Setter
        Property="FontFamily"
        Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter
        Property="FontSize"
        Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter
        Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter
        Property="Template">
        <Setter.Value>
            <ControlTemplate
                TargetType="ListBox">
                <Border
                    x:Name="LayoutRoot"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup
                            x:Name="CommonStates">
                            <VisualState
                                x:Name="Normal" />
                            <VisualState
                                x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetProperty="Background"
                                        Storyboard.TargetName="LayoutRoot">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="Transparent" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetProperty="BorderBrush"
                                        Storyboard.TargetName="LayoutRoot">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{ThemeResource ListBoxDisabledForegroundThemeBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup
                            x:Name="FocusStates">
                            <VisualState
                                x:Name="Focused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames
                                        Storyboard.TargetProperty="Background"
                                        Storyboard.TargetName="LayoutRoot">
                                        <DiscreteObjectKeyFrame
                                            KeyTime="0"
                                            Value="{ThemeResource ListBoxFocusBackgroundThemeBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState
                                x:Name="Unfocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ScrollViewer
                        x:Name="ScrollViewer"
                        AutomationProperties.AccessibilityView="Raw"
                        BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}"
                        HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                        HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                        IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                        IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                        IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                        Padding="{TemplateBinding Padding}"
                        TabNavigation="{TemplateBinding TabNavigation}"
                        VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                        VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                        ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}">
                        <ItemsPresenter />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>