wpf中有圆角的listbox

本博客展示了如何在wpf中为ListBox元素添加圆角边框,并通过样式定义了ListBox的基本外观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:wpf中有圆角的listbox


  1.  <Style TargetType="{x:Type ListBox}">  
  2.          <!-- 定义ListBox自身外观, 比如: 圆角边框-->  
  3.          <Setter Property="Template">  
  4.              <Setter.Value>  
  5.                  <ControlTemplate TargetType="ListBox">  
  6.                      <Border CornerRadius="5" Background="pink">  
  7.                          <ScrollViewer HorizontalScrollBarVisibility="Hidden">  
  8.                              <ItemsPresenter/>  
  9.                          </ScrollViewer>  
  10.                      </Border>  
  11.                  </ControlTemplate>  
  12.              </Setter.Value>  
  13.          </Setter>  
  14.      </Style>  
  15.   
  16. <ListBox Height="50" HorizontalAlignment="Left" Margin="236,84,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" >  
  17.          <ListBoxItem>wendyOne</ListBoxItem>  
  18.          <ListBoxItem Foreground="White">wendyTwo</ListBoxItem>  
  19.          <ListBoxItem>wendThree</ListBoxItem>  
  20.      </ListBox>  

### WPF 中自定义 ListBox 的边框样式 在 WPF 中,可以通过 `ItemContainerStyle` 和模板来实现对 `ListBox` 边框样式的完全控制。以下是详细的解决方案: #### 使用 `ItemContainerStyle` 定制边框 通过设置 `ItemContainerStyle` 属性,可以更改每个 `ListBoxItem` 的外观,包括其边框颜色、宽度和其他视觉效果。 ```xml <ListBox> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <!-- 自定义边框 --> <Border x:Name="itemBox" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="5" Padding="5"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <!-- 当项目被选中时改变边框颜色 --> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="itemBox" Property="BorderBrush" Value="Blue"/> <Setter TargetName="itemBox" Property="Background" Value="LightSkyBlue"/> </Trigger> <!-- 鼠标悬停时的效果 --> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="itemBox" Property="BorderBrush" Value="Red"/> <Setter TargetName="itemBox" Property="Background" Value="Pink"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> </ListBox> ``` 以上代码展示了如何定制 `ListBox` 的边框样式[^1]。具体来说: - **默认状态**:设置了灰色的边框 (`BorderBrush`) 和圆角 (`CornerRadius`)。 - **选中状态**:当某个项被选中时,边框变为蓝色,背景色也发生变化。 - **鼠标悬停状态**:当鼠标移动到某一项上方时,边框会变成红色,背景色也会相应变化。 #### 应用全局样式资源 如果希望在整个应用程序中统一应用这种样式,则可以在 `Resources` 节点下定义一个静态资源并引用它。 ```xml <Window.Resources> <Style x:Key="CustomListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="itemBox" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="5" Padding="5"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="itemBox" Property="BorderBrush" Value="Blue"/> <Setter TargetName="itemBox" Property="Background" Value="LightSkyBlue"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="itemBox" Property="BorderBrush" Value="Red"/> <Setter TargetName="itemBox" Property="Background" Value="Pink"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <!-- 在 ListBox 上绑定该样式 --> <ListBox ItemContainerStyle="{StaticResource CustomListBoxItemStyle}" /> ``` 这样做的好处是可以重用相同的样式而无需重复编写代码[^2]。 #### 解决常见问题 有时可能会遇到一些常见的边框显示问题,比如: - 如果发现某些情况下边框未按预期渲染,请检查是否存在其他覆盖样式的冲突。 - 确保没有额外的外层控件影响布局行为 (例如 Grid 或 StackPanel)[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值