选择项目背景?

时间:2013-08-14 13:14:50

标签: wpf listbox togglebutton

我有ListBox个按钮DataTemplateListBox个查看项目位于ToggleButton个列表中。

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

enter image description here

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

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

   <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>

有关如何解决此问题的任何建议吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>
相关问题