zoukankan      html  css  js  c++  java
  • WPF Litbox样式和模板

    1、在项目中使用ListBox时,经常会将ItemContainerStyle和ItemTemplate的作用搞混,ItemTemplate可以搞定一切好似ItemContainerStyle有点多余。我们再来看下ItemContainerStyle和ItemTemplate。 ItemContainerStyle用于给每个Item的容器定义样式,其类型是Style。包含了操作Item的Triggers。 ItemTemplate是每个Item的现实样式,其类型是DataTemplate

    在实际应用中,我们往往需要根据用户操作不断的改变ListBox中Items的显示样式。这里 总结一下ListBox中3种应用场景中的最优解决方案。

    A.选中Item时改变Item项的背景颜色。

    S.在 ItemContainerStyle中写ControlTemplate的样式给定背景色,使用Trigger在Selected为True时改变背景色,代码如下:

            <Style x:Key="containerstyle1" TargetType="{x:Type ListBoxItem}">

                <Setter Property="Template" >

                    <Setter.Value>

                        <ControlTemplate TargetType="{x:Type ListBoxItem}">

                            <Border x:Name="b1" Background="Red">

                                <ContentPresenter></ContentPresenter>

                            </Border>

                            <ControlTemplate.Triggers>

                                <Trigger Property="IsSelected" Value="True">

                                    <Setter Property="Background" Value="Green" TargetName="b1"></Setter>

                                </Trigger>

                            </ControlTemplate.Triggers>

                        </ControlTemplate>

                    </Setter.Value>

                </Setter>

            </Style> 

    B. 选中Item时改变Item项的部分显示。

    S. 使用改变变量的方式,代码如下:

             <Style x:Key="containerstyle1" TargetType="{x:Type ListBoxItem}">

                <Setter Property="Template" >

                    <Setter.Value>

                        <ControlTemplate TargetType="{x:Type ListBoxItem}">

                            <Border x:Name="b1" Background="Red">

                                <Label x:Name="l1" Margin="5" Content="{Binding A1}" FontSize="26" Foreground="DarkBlue"></Label>

                            </Border>

                            <ControlTemplate.Triggers>

                                <Trigger Property="IsSelected" Value="True">

                                    <Setter Property="Content" Value="{Binding A2}" TargetName="l1"></Setter>

                                </Trigger>

                            </ControlTemplate.Triggers>

                        </ControlTemplate>

                    </Setter.Value>

                </Setter>

            </Style>

    C. 选中Item时需要完全改变Item呈现的样式,如圆形变成矩形等等。 

    S. 改变Item项的DataTemplate,代码如下:

                <DataTemplate x:Key="ItemTemplate" DataType="{x:Type ListBoxItem}">... </DataTemplate>

                <DataTemplate x:Key="SelectedTemplate" DataType="{x:Type ListBoxItem}">... </DataTemplate> 

                <Style x:Key="SeatListStyle" TargetType="{x:Type ListBoxItem}">

                    <Setter Property="Template">

                        <Setter.Value>

                            <ControlTemplate TargetType="{x:Type ListBoxItem}">

                                <ContentPresenter></ContentPresenter>

                                <ControlTemplate.Triggers>

                                    <Trigger Property="IsSelected" Value="True">

                                        <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />

                                    </Trigger>

                                </ControlTemplate.Triggers>

                            </ControlTemplate>

                        </Setter.Value>

                    </Setter>

                    <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}"></Setter>

                </Style> 

    2、使用举例

    <ListBox  
                    Name="ChangeAdListbox" 
                    Grid.Row="0"
                    Background="#77000000"                        
                    VerticalAlignment="Bottom" 
                    HorizontalAlignment="Right"   
                     ItemsSource="{Binding Items,ElementName=flipview}"
                    SelectionChanged="ChangeAdListbox_SelectionChanged"
                    Margin="0,0,20,5" d:IsHidden="True"                 
                    >
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="Background" Value="White"/>
                            <Setter Property="Width" Value="12"/>
                            <Setter Property="Height" Value="12"/>
                            <Setter Property="Margin" Value="4"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListBoxItem">
                                        <Border x:Name="bd" BorderThickness="1" BorderBrush="White" Background="{TemplateBinding Background}">
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsSelected" Value="True">
                                                <Setter Property="Background" Value="SkyBlue"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>
  • 相关阅读:
    How To Make Your Software Successful with Software Review Sites?
    8368U编译
    Readiness probe failed: Client.Timeout exceeded while awaiting headers)
    K8S为 Pod 或容器配置安全上下文securityContext,解决Docker容器限制文件句柄数的问题
    kubernetes使用securityContext和sysctl
    golang正则表达式在线匹配检测
    Atitit 软件开发体系法规大全v2.docx Atitit 软件开发体系大全 目录 1. 基本法(类似宪法) 1 2. 行政法 1 3. 流程法民商法 2 3.1. Ui提升法 2 3.2. 功
    Atitit 软件开发基本法 目录 1. 第一章 总纲(包含各大原则 定律 法则) 1 2. 爱提拉的原则 3 2.1. 简单原则 KISS 3 2.2. 提升可读性 面向人类编程 而不是面向机
    Atitit Cookie安全法 目录 1. cookie分为 会话cookie 和 持久cookie , 1 1.1. 安全措施 1 1.2. 3. cookie的同源策略 2 1.3. 安全类库
    Atitit 外观ui调整法 表单与表格列表
  • 原文地址:https://www.cnblogs.com/fuchongjundream/p/4108368.html
Copyright © 2011-2022 走看看