WPF ListBoxItem鼠标越过边框更改

WPF ListBoxItem鼠标越过边框更改,wpf,xaml,listbox,Wpf,Xaml,Listbox,我正在与WPF中的ListBox控件的样式进行斗争 如果鼠标位于ListBoxItem上,我想更改item的BorderBrush属性 我的列表框是自定义控件的一部分,但下面是一些代码: <ListBox x:Name="suggestionListBox" SelectionChanged="suggestionListBox_SelectionChanged" MouseUp="SuggestionListBox_OnMouseDown"

我正在与WPF中的ListBox控件的样式进行斗争

如果鼠标位于ListBoxItem上,我想更改item的
BorderBrush
属性

我的列表框是自定义控件的一部分,但下面是一些代码:

<ListBox x:Name="suggestionListBox"
            SelectionChanged="suggestionListBox_SelectionChanged"
            MouseUp="SuggestionListBox_OnMouseDown"
            Background="{Binding ElementName=Control, Path=Background}"
            ItemTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:AutoCompleteComboBox}}}"
            Width="{Binding ElementName=Control, Path=ActualWidth}"
            HorizontalContentAlignment="Stretch">


    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Margin" Value="0" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="BorderThickness" Value="1"></Setter>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderBrush" Value="Yellow" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="Green" />
                </Trigger>
            </Style.Triggers>


        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

我添加的ItemTemplate如下所示:

<DataTemplate>

        <StackPanel>
            <Label Content="{Binding FullName}" />
        </StackPanel>

</DataTemplate>

基本上,ListBoxItem内部有一个边框,我无法访问它,当IsMouseOver设置为true时,它会发生变化


如何在鼠标悬停或未悬停时更改边框的颜色?

您可以尝试设置列表框的系统颜色并覆盖现有默认值(如HighlightBrushColor),或者,如果使用VisualStudio,右键单击列表框->编辑模板->编辑副本并根据需要修改此模板。尤其是当您希望向列表框添加更多自定义行为时。

您可以尝试为列表框设置System.Color并覆盖现有默认值(如HighlightBrushColor),或者,如果使用VisualStudio,右键单击列表框->编辑模板->编辑副本并根据需要修改此模板。尤其是当你想在列表框中添加更多定制行为时。

多亏了Jayasri,我终于做到了:

<DataTemplate>
    <Border CornerRadius="0,3,3,0">
        <Border.Style>
            <Style TargetType="Border">
                <Setter Property="Border.Background" Value="#0093DD"/>
                <Style.Triggers>
                    <Trigger Property="Border.IsMouseOver" Value="True">
                        <Setter Property="Border.Background" Value="#70116b" />
                        <Setter Property="Label.Foreground" Value="White" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>

    <StackPanel>
            <Label Content="{Binding FullName}" >
                <Label.Style>
                    <Style TargetType="Label">
                        <Style.Triggers>
                            <Trigger Property="Border.IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="White"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Label.Style>
                </Label>
        </StackPanel>
    </Border>
</DataTemplate>

多亏了Jayasri,我做到了这一点:

<DataTemplate>
    <Border CornerRadius="0,3,3,0">
        <Border.Style>
            <Style TargetType="Border">
                <Setter Property="Border.Background" Value="#0093DD"/>
                <Style.Triggers>
                    <Trigger Property="Border.IsMouseOver" Value="True">
                        <Setter Property="Border.Background" Value="#70116b" />
                        <Setter Property="Label.Foreground" Value="White" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>

    <StackPanel>
            <Label Content="{Binding FullName}" >
                <Label.Style>
                    <Style TargetType="Label">
                        <Style.Triggers>
                            <Trigger Property="Border.IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="White"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Label.Style>
                </Label>
        </StackPanel>
    </Border>
</DataTemplate>

试试这个

<Border Name="ListboxBorderr" Grid.Column="1" CornerRadius="0,3,3,0">
  <Border.Style> 
    <Style> 
      <Setter Property="Border.Background" Value="Blue"/>

      <Style.Triggers> 
        <Trigger Property="Border.IsMouseOver" Value="True"> 
          <Setter Property="Border.Background" Value="Green" />
        </Trigger> 
      </Style.Triggers> 
    </Style> 
  </Border.Style> 
</Border>

试试这个

<Border Name="ListboxBorderr" Grid.Column="1" CornerRadius="0,3,3,0">
  <Border.Style> 
    <Style> 
      <Setter Property="Border.Background" Value="Blue"/>

      <Style.Triggers> 
        <Trigger Property="Border.IsMouseOver" Value="True"> 
          <Setter Property="Border.Background" Value="Green" />
        </Trigger> 
      </Style.Triggers> 
    </Style> 
  </Border.Style> 
</Border>


默认ListBoxItem样式中的ControlTemplate可能不使用BorderBrush属性。您应该创建自己的ControlTemplate。看看MSDN上的页面@Jayasri我应该把这个放在哪里?xaml页面在样式部分是的,非常感谢默认ListBoxItem样式中的ControlTemplate可能只是不使用BorderBrush属性。您应该创建自己的ControlTemplate。看看MSDN上的页面@Jayasri我应该把这个放在哪里?xaml页面里面样式部分是的,非常感谢