天天看點

WPF 自定義ListBox MaterialDesign

<ListBox>
            <ListBoxItem >
                <StackPanel Height="60" Orientation="Horizontal" Margin="20 0 40 0">
                    <Image Source="/Images/圖檔.png" Stretch="None" />
                    <TextBlock Text="文本" VerticalAlignment="Center" Foreground="White" FontSize="14"/>
                </StackPanel>
            </ListBoxItem>


            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Grid x:Name="panel">
                                    <wpf:Ripple Focusable="False" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="False" Feedback="{TemplateBinding Foreground , Converter={StaticResource BrushRoundConverter}}" />
                                </Grid>

                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Cursor" Value="Hand"/>
                                        <Setter TargetName="panel" Property="Background" Value="#3792FD"/>
                                    </Trigger>

                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="panel" Property="Background" Value="#1E69C6"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>