【问题标题】:Disable list box selected item color禁用列表框选定项颜色
【发布时间】:2014-01-26 18:26:54
【问题描述】:

我正在使用 istBox,我想禁用它的颜色,当你 选定的项目(默认为蓝色)我试过这样 但是当您选择项目时,您看不到它...,知道如何禁用颜色/

<ListBox.Resources>
            <!-- Background of selected item when focussed -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                     Color="Transparent" />


        </ListBox.Resources>

更新

            x:Name="LstUsers" Height="115" Width="150" 

            ItemsSource="{Binding UsersCollectionView}" 
                 AllowDrop="True" 
                  PreviewKeyDown="ListBoxPreviewKeyDown"  
                 PreviewDrop="ListBox_PreviewDrop"
                 PreviewMouseDown="listBox_PreviewMouseMove"
                 Margin="45,119,805,445"
                 MouseUp="listBox_MouseUp"
                 PreviewMouseLeftButtonDown="listBox_PreviewMouseLeftButtonDown"


                  Drop="ListBox_Drop">


        </ItemsControl>

【问题讨论】:

    标签: wpf xaml listbox


    【解决方案1】:

    使用ItemsControl 代替ListBox。

    它没有你为 ListBox 提供的所有那些花哨的模板(包括没有突出显示颜色)。


    就像你在评论中提到的你想要SelectionChanged 事件,不幸的是not in ItemsControl。禁用选择突出显示颜色的唯一其他方法是覆盖 ListBoxItem 的模板。这就是你的做法:

    <ListBox>
       <ListBox.ItemContainerStyle>
          <Style TargetType="ListBoxItem">
             <Setter Property="Template">
                <Setter.Value>
                   <ControlTemplate TargetType="ListBoxItem">
                       <ContentPresenter
                           Content="{TemplateBinding ContentControl.Content}"
                           ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                           ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                           HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                           VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                           SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                    </ControlTemplate>
                </Setter.Value>
             </Setter>
          </Style>
       </ListBox.ItemContainerStyle>
    </ListBox>
    

    【讨论】:

    • 谢谢,目前我在列表框中使用以下代码,如果我使用项目控件 UserList.Remove(LstUsers.SelectedItem as User),我应该如何更改它;
    • 只需将ListBox 替换为ItemsControl 即可。在这种情况下无需覆盖 HighlightBrush,因为默认情况下它已经是透明的。 ItemsControl 是 ListBox 的基类,没有我在回答中提到的任何花哨的模板。
    • D&D 事件也可以在 ItemsControl 上完成,因为它是 ListBox 的基类。你试过了吗?
    • 但是现在当我更改它时,D&D 事件无法正常工作..我无法将项目删除为 beefore
    • 我们需要查看不起作用的代码,否则它将只是空话。
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2014-09-16
    相关资源
    最近更新 更多