2011年6月5日 星期日

在 WPF 如何改變 ListBox 已選擇項目的樣式?

關於這個問題我也搞了好久,以下是完整範例:

其實重點在於要改變 ListBoxItem 的 Template 屬性,然後指定 IsSelected 屬性為 True 時的樣式。


<Window x:Class="Project_DependencyProperty.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Project_DependencyProperty"
        Title="MainWindow" Height="513" Width="526">
    <Window.Resources>
        <local:TaskCollection x:Key="Tasks"/>
        <DataTemplate DataType="{x:Type local:Task}">
            <Border BorderThickness="1" BorderBrush="Blue" Margin="2" Padding="2">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80*"/>
                        <ColumnDefinition Width="5*"/>
                        <ColumnDefinition Width="15*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name}" Grid.Column="0"/>
                    <TextBlock Text=":" Grid.Column="1"/>
                    <TextBlock Text="{Binding Type}" Grid.Column="2"/>
                </Grid>
            </Border>
        </DataTemplate>
        <Style TargetType="ListBoxItem" x:Key="SelectedItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border Background="Gray"
                            CornerRadius="8"
                            BorderThickness="3"
                            x:Name="LBI"
                            Margin="0" >
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="LBI" Property="Background">
                                    <Setter.Value>Yellow</Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="LightBlue"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox HorizontalContentAlignment="Stretch" Margin="17,18,10,13"
                 Name="listBox1" FontWeight="Normal" FontStyle="Normal"
                 ItemContainerStyle="{DynamicResource SelectedItem}"
                 SelectionMode="Extended">
            <ListBox.ItemsSource>
                <Binding Source="{StaticResource Tasks}"/>
            </ListBox.ItemsSource>
        </ListBox>
        <Grid.LayoutTransform>
            <ScaleTransform ScaleX="1.5" ScaleY="1.5"/>
        </Grid.LayoutTransform>
    </Grid>
</Window>