WPF中ListBox的樣式設置


設置之后的效果為

 

1 窗體中代碼

<Window x:Class="QyNodeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/Style.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ListBox Style="{StaticResource ListBoxHor}">
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">項目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">項目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">項目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
            <ListBoxItem>
                <Border>
                    <StackPanel>
                        <TextBlock HorizontalAlignment="Center">項目1</TextBlock>
                        <Button Content="OK" Width="80" HorizontalAlignment="Center"/>
                    </StackPanel>
                </Border>
            </ListBoxItem>
        </ListBox>
    </Grid>
</Window>

2 樣式文件中代碼

<!--設置ListBox樣式-->

<Style TargetType="ListBox" x:Key="ListBoxHor">

    <!--設置模板-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                        <WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


   <!--設置ListBoxItem樣式-->
    <Style TargetType="ListBoxItem">
        <Setter Property="Width" Value="120"></Setter>
        <Setter Property="Height" Value="40"></Setter>
        <Setter Property="Margin" Value="5"></Setter>
        <Setter Property="BorderBrush" Value="Red"/>
        <Setter Property="BorderThickness" Value="1"/>
        <!--設置控件模板-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" TextBlock.Foreground="{TemplateBinding Foreground}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!--設置觸發器-->
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="#808080"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="BorderBrush" Value="Green"/>
                <Setter Property="BorderThickness" Value="2"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="BorderBrush" Value="Black"/>
                <Setter Property="BorderThickness" Value="2"/>
            </Trigger>
        </Style.Triggers>
    </Style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM