Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择编辑项样式don';无法使用DataTemplate处理ListBox?_C#_Wpf_Listbox - Fatal编程技术网

C# 选择编辑项样式don';无法使用DataTemplate处理ListBox?

C# 选择编辑项样式don';无法使用DataTemplate处理ListBox?,c#,wpf,listbox,C#,Wpf,Listbox,我有一个带有数据模板的列表框,我需要更改所选项目的背景色。我已经找到了一些例子,并尝试了所有这些,但可能我错过了一些要点 我试图更改SolidColorBrush资源,并在列表框中为IsSelected和ListBoxItem样式添加触发器,但这些都不起作用 什么是正确的样式在何处添加触发器或更改SolidColorBrush资源?最简单的方法是去掉为ListBoxItem声明的样式,而是在数据模板中使用数据触发器列表框: (不要忘记将x:Name=“grid”添加到您的网格中) Rel

我有一个带有
数据模板的
列表框
,我需要更改所选项目的背景色。我已经找到了一些例子,并尝试了所有这些,但可能我错过了一些要点

我试图更改
SolidColorBrush
资源,并在
列表框中为
IsSelected
ListBoxItem
样式添加触发器,但这些都不起作用



什么是正确的
样式
在何处添加触发器或更改
SolidColorBrush
资源?

最简单的方法是去掉为
ListBoxItem
声明的
样式
,而是在
数据模板
中使用
数据触发器
列表框


(不要忘记将
x:Name=“grid”
添加到您的
网格中

RelativeSource
绑定允许WPF查找可视化树,直到找到一个
ListBoxItem
(它会找到,因为您正在模板化每个
ListBoxItem
内容)

这将把
ListBoxItem
内容的
Background
属性设置为
Red
。它不会更改
ListBoxItem
本身的
Background
属性

如果这正是您想要的,则更复杂但功能最强大的选项是使用
样式
完全覆盖
ListBoxItem
模板
属性:


通过这种方式,您可以在所有状态下重新定义每个属性(
选定的
未选定的
聚焦的
等)。 这仅仅是一个灵感来源的起点


遗憾的是,这不可能只覆盖
控制模板的一部分。如果您重写它,您需要重写它的每个部分,甚至是您不希望更改的部分。

您应该更改样式“ListBoxDragDrop”,添加默认的ItemContainerStyle。然后,您可以根据需要更改SolidColorBrush

<Style x:Key="FocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/>
<SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="LightGreen"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="Green"/>
<SolidColorBrush x:Key="Item.SelectedActive.Background" Color="LightGreen"/>
<SolidColorBrush x:Key="Item.SelectedActive.Border" Color="Green"/>

<Style x:Key="ListBoxDragDrop" TargetType="{x:Type ListBoxItem}">
    <Setter Property="AllowDrop" Value="true"/>
    <EventSetter Event="PreviewMouseMove" Handler="ListBox_PreviewMouseMove"/>
    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBox_PreviewMouseLeftButtonDown"/>
    <EventSetter Event="Drop" Handler="ListBox_Drop"/>

    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Padding" Value="4,1"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="False"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


非常感谢您的详细回答。我尝试了您建议的第一个解决方案,但它改变了网格颜色,而不是项目背景。它可以满足我的需要,但问题是网格比项目要短,并且背景是绿色的,仅适用于网格的大小。我不知道如何设置宽度以将网格扩展到项目的大小。是的,这就是为什么我认为您必须使用第二种解决方案,即WPF重新部署datagrid的方式。我尝试了第二种方法,它在背景下非常有效,但我失去了鼠标悬停效果。我必须把它添加到样式中吗?有一种方法可以保持默认样式并仅更改项目背景?@user12338473遗憾的是,这不可能仅覆盖
控制模板的一部分。您确实需要从鼠标的某个地方重写或复制粘贴:/极好的问题:它适用于
组合框
,因为默认模板中有一个
Background=“{TemplateBinding Background}”
,允许您以简单的样式.Trigger更改
Background
属性。对于默认的
ListBoxItem
模板,情况并非如此。非常感谢!这正是我需要的款式!