WPF ListBox样式编辑

1.       去掉ListBox中选中状态

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
  <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
    <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ListBoxItem}">
        <Border x:Name="Border" Background="{TemplateBinding Background}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
          <ContentPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />         </Border>         <ControlTemplate.Triggers>           <Trigger Property="IsSelected" Value="True">             <Setter Property="Background" Value="Transparent" />             <Setter Property="Foreground" Value="Transparent" />           </Trigger>           <Trigger Property="IsMouseOver" Value="True">             <Setter Property="Background" Value="Transparent" />             <Setter Property="Foreground" Value="Transparent" />           </Trigger>         </ControlTemplate.Triggers>       </ControlTemplate>     </Setter.Value>   </Setter> </Style> <Style TargetType="ListBox" x:Key="ListBoxStyle">   <Setter Property="Background" Value="Transparent"></Setter>   <Setter Property="BorderBrush" Value="#FF096B9C"></Setter>   <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>   <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
  <Setter Property="FocusVisualStyle" Value="{x:Null}"></Setter>   <Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemStyle}"></Setter> </Style>

2.ListBox 2行N列自适应

<Style TargetType="ListBox" x:Key="ListBoxStyle">
  <Setter Property="Background" Value="Transparent"></Setter>
  <Setter Property="BorderBrush" Value="#FF096B9C"></Setter>
  <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
  <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
  <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"></Setter>
  <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"></Setter>
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <UniformGrid Rows="2" Columns="5" IsItemsHost="True" Focusable="True"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
</Style>

3.ListBox 控件宽高自适应Item项宽高

<Style TargetType="ListBox" x:Key="ListBoxStyle">
  <Setter Property="Background" Value="Transparent"></Setter>
  <Setter Property="BorderBrush" Value="Transparent"></Setter>
  <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
  <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
  <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"></Setter>
  <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"></Setter>
  <Setter Property="ItemsPanel">
    <Setter.Value>
      <ItemsPanelTemplate>
        <WrapPanel HorizontalAlignment="Center" Orientation="Vertical" IsItemsHost="True" Focusable="True"/>
          </ItemsPanelTemplate>
            </Setter.Value>
    </Setter>
</Style>
<ListBox Style="{StaticResource ListBoxStyle}" ItemsSource="{Binding PlayerList}" SelectionMode="Multiple">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Border>
          <control:Machine Width="{Binding ElementName=PlayerList,Path=ActualWidth,Converter={StaticResource ListBoxItemWidthConverter}}"
                                  Height="{Binding ElementName=PlayerList,Path=ActualHeight,Converter={StaticResource ListBoxItemHeightConverter}}">
          </control:Machine>
        </Border>
      </Grid>
    </DataTemplate>
  </ListBox.ItemTemplate>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
      <i:InvokeCommandAction Command="{Binding PlayerSelectChangeCommand}" CommandParameter="{Binding SelectedItems,ElementName=PlayerList}"/>
      </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>
public class ListBoxItemHeightConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    var dValue = value as double?;
    return dValue / 4 * 0.9;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    return null;
  }
}

public class ListBoxItemWidthConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    var dValue = value as double?;
    return dValue * 0.9 - 20;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    return null;
  }
}

 

转载于:https://www.cnblogs.com/throbing/p/5702658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值