【问题标题】:Selected Item Background?所选项目背景?
【发布时间】:2013-08-14 23:24:23
【问题描述】:

我有一个ListBox 和一个按钮DataTemplateListBox 视图项位于ToggleButtons 列表中。

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

如图所示,问题出在按钮一上。此屏幕截图是在首次加载 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>

关于如何解决这个问题的任何建议?

【问题讨论】:

  • 你能把你正在使用的风格贴出来吗?
  • 您已经很好地解释了您的问题,但如果您发布用于在列表框中显示这些按钮的整个 XAML,这将有很大帮助。
  • 我已经用列表框的 XAML 代码更新了我的问题

标签: wpf listbox togglebutton


【解决方案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

【讨论】:

  • 还是同样的问题 :(
  • 我刚刚将您的 XAML 加载到一个新的 WPF 项目中,一切正常...我可以看到按钮后面没有白色背景。
  • 白色背景仅在首次加载时出现,在检查按钮后它消失了,无论如何,我已经添加了答案并感谢您的时间。
  • 嗨,Sheridan,我的问题被标记为重复。但我不能用你的回答来解决。我该如何继续?
【解决方案2】:

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

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

【讨论】:

  • 我建议您在线阅读一些 WPF 书籍或教程。有 方式 更简单的方法可以实现您想要的。对于其他 SO 用户...请继续搜索,因为此解决方案远非最佳。
  • 我同意你的观点,它可能不是最佳解决方案,但它有效,理论上你输入的解决方案是合乎逻辑的,但它不起作用。
  • 哦,拜托...我的评论与我的回答完全无关...看起来我需要声誉积分吗?我只是添加了评论,因为不需要声明 ControlTemplate 或使用 ContentControl 来实现您想要的。我只是不想让其他用户认为这是最好的方法。如果我伤害了你的感情,我深表歉意。
  • Sheridan,不用道歉,目标是达到最优解。完全没有问题,我仍然期待一个完美的解决方案,如果你得到它希望你与我分享,我会非常感谢你帮助我。
猜你喜欢
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多