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