【问题标题】:WPF ListBoxItem MouseOver border changeWPF ListBoxItem MouseOver 边框更改
【发布时间】:2015-04-17 10:07:27
【问题描述】:

我正在与 WPF 中的 ListBox 控件样式作斗争。

如果我的鼠标悬停在 ListBoxItem 上,我想更改项目的 BorderBrush 属性。

我的 ListBox 是自定义控件的一部分,但这里有一些代码:

<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 时,边框会发生变化。

鼠标悬停时如何改变边框的颜色?

【问题讨论】:

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

标签: wpf xaml listbox


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    您可以尝试为 ListBox 设置 System.Colors 并覆盖现有的默认值(如 HighlightBrushColor),或者,如果您使用 VisualStudio,请右键单击您的 ListBox -> 编辑模板 -> 编辑副本并将此模板修改为你的愿望。特别是当您想向 ListBox 添加更多自定义行为时。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 2018-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多