1

我有一个ListBox带按钮的DataTemplateListBox视图项位于 s 列表中ToggleButton

我的问题是如图所示的选定项目背景:

在此处输入图像描述

如图所示,问题出在按钮一上。此屏幕截图是在首次加载 wpf 页面时拍摄的(在选中切换按钮之前)。如果我然后检查切换按钮(按钮一),边框中的白色将消失

我不希望显示这种白色(如两个按钮)。我有:

   <ListBox x:Name="lbname" ItemsSource="{Binding myCollection}" Margin="432,110,0,158" Width="214" HorizontalAlignment="Left" Background="{x:Null}" BorderBrush="{x:Null}">
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Style.Resources>
                    <!-- SelectedItem with focus -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                   Color="Transparent" />
                    <!-- SelectedItem without focus -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                   Color="Transparent" />
                    <!-- SelectedItem text foreground -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
                   Color="Black" />
                </Style.Resources>
                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            </Style>
        </ListBox.Resources>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <ToggleButton x:Name="btnname" tyle="{StaticResource ToggleStyle}" FontFamily="tahoma" FontSize="14" Height="55" Width="208" FontWeight="Normal" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Background="{x:Null}" BorderBrush="{x:Null}" Checked="btnname_Checked" >
                    <Grid>
                        <Image Height="55" Width="205">
                            <Image.Style>
                                <Style>
                                    <Setter Property="Image.Source" Value="/myApplication;component/images/buttons/two.png" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
                                            <Setter Property="Image.Source" Value="/myApplication;component/images/buttons/one.png" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                        <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Grid>
                </ToggleButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

对于如何解决这个问题,有任何的建议吗?

4

2 回答 2

1

您需要添加它Style以覆盖的默认样式ListBoxItem

<Style x:Key="HiddenDefaultSelectionStyle" TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
    </Style.Resources>
</Style>

您当然可以用您喜欢的任何颜色替换这些颜色。你可以像这样使用它:

<ListBox ItemContainerStyle="{StaticResource HiddenDefaultSelectionStyle}" />

或者您可以删除x:Key="HiddenDefaultSelectionStyle"声明,然后它将影响ListBoxItem范围内的每个。

于 2013-08-14T13:47:40.940 回答
0

好的,最后我通过使用以下样式解决了我的问题

<Style TargetType="ListBoxItem" x:Key="NoSelectionItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border x:Name="LayoutRoot" 
                    BorderBrush="{TemplateBinding BorderBrush}" 
                    BorderThickness="{TemplateBinding BorderThickness}" 
                    Background="{TemplateBinding Background}" 
                    HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                    VerticalAlignment="{TemplateBinding VerticalAlignment}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="Disabled" />
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected" />
                            <VisualState x:Name="Selected" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <ContentControl x:Name="ContentContainer"
                                    ContentTemplate="{TemplateBinding ContentTemplate}" 
                                    Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" 
                                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                    Margin="{TemplateBinding Padding}" 
                                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>

                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2013-08-14T14:26:34.990 回答