【WPF】修改ListBox的Item的樣式

<ListBox>
        <!-- 數據 -->
        <ListBoxItem>AAAA</ListBoxItem>
        <ListBoxItem>BB</ListBoxItem>
        <ListBoxItem>CCCC</ListBoxItem>
       
        <!-- 設置ListBoxItem樣式 -->
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <!-- 設置控件模板 -->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                 TextBlock.Foreground="{TemplateBinding Foreground}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
               
                <!-- 設置觸發器 -->
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="LightPink"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="LightGreen"/>
                        <Setter Property="Foreground" Value="Red"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
相關文章
相關標籤/搜索