【问题标题】:Change style for all ListBoxItem in a ListBox (Windows phone 8)更改 ListBox 中所有 ListBoxItem 的样式(Windows phone 8)
【发布时间】:2013-04-08 23:15:05
【问题描述】:

我有一个带有一些 Listboxitem 的 Listbox,我想更改所有项目的样式。我知道,可以在资源中创建一个样式并将这个样式绑定到每个项目,但也许有可能更容易做到(没有绑定)?使用 ListBox.ItemTemplate?

              <ListBox SelectionChanged="ListBox_SelectionChanged">
                        <ListBoxItem x:Name="ItemAdress">
                                ....
                        </ListBoxItem>

                        <ListBoxItem x:Name="ItemPhone">
                                ....
                        </ListBoxItem>

                        <ListBoxItem x:Name="ItemEmail">
                                ....
                        </ListBoxItem>
              </Listbox>

事实上,我的目标是为每个项目添加 Margin bottom 15。 (在项目之间添加一个空格)

【问题讨论】:

    标签: c# xaml listbox windows-phone-8 listboxitem


    【解决方案1】:

    注意:这适用于 WPF;不确定它是否也适用于 Windows Phone。

    您可以使用 TargetType 创建样式。这将影响此特定 ListBox 的所有 ListBoxItems:

    <ListBox Height="200" Width="200">
      <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
          <Setter Property="Margin" Value="0,0,0,15" />
        </Style>
      </ListBox.Resources>
      <ListBoxItem x:Name="ItemAdress">
        ABC
      </ListBoxItem>
      <ListBoxItem x:Name="ItemPhone">
        DEF
      </ListBoxItem>
      <ListBoxItem x:Name="ItemEmail">
        GHI
      </ListBoxItem>
    </ListBox>
    

    如果您想为 所有 ListBox 设置 ListBoxItem 的样式,您可以在适当的父级别指定样式。

    例如以下适用于此 Grid 下所有 ListBox 的所有 ListBoxItems 的样式。

    <Grid>
      <Grid.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
          <Setter Property="Margin" Value="0,0,0,15" />
        </Style>
      </Grid.Resources>
      <ListBox x:Name="listBox1" Height="200" Width="200">
        ...
      </ListBox>
      <ListBox x:Name="listBox2" Height="200" Width="200">
        ...
      </ListBox>
    </Grid>
    

    【讨论】:

    • 谢谢。有用。只是与 TargetType 的一个小区别。 &lt;Style TargetType="ListBoxItem"&gt;
    【解决方案2】:

    在 publicgk 答案的基础上,我想展示如何“简单”地做到这一点,以及在 RE 中添加更多信息到样式。如果您已经知道我要说的内容,那么我很抱歉 - 我添加这个以防万一有人正在查看这个问题,因此不熟悉 WP8 或 XAML 开发。

    如果您想设置 data 显示方式的样式,那么我建议您创建一个 DataTemplate 并将 ListBox.ItemTemplate 设置为该数据模板(正如您上面建议的那样)。

    您可以内联执行此操作,或者将 DataTemplate 作为资源并按名称引用它。

                <!--ItemsSource set in code-->
            <ListBox>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border Background="Blue">
                            <TextBlock Text="{Binding Title}"/>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
    
            <!--ListOfTitles is a property in the code-->
            <ListBox ItemsSource="{Binding ListOfTitles}" ItemTemplate="{StaticResource redDataTemplateDefinedInResources}">
            </ListBox>
    

    请注意,您需要设置 itemssource,这可以在代码隐藏中完成,方法是通过为其命名 (x:name="theName") 访问 ListBox 或使用绑定并绑定到具有该名称的属性(make确保也设置 DataContext)

    如果您想设置默认 ListBoxItem 的样式(例如交互、边距等),那么您可以 publicgk 编写使用控件目标类型创建样式并将其设置为父级别、页面或应用级别的资源。对于整个应用程序中的所有控件,使用应用程序级别- 与资源字典保持一切整洁:)

    这是创建 FULL 模板以覆盖 Visual Studio 2012 或 Blend 中的样式的最简单方法: 右键单击设计器或文档大纲窗口中的控件,然后选择编辑模板,编辑副本,如果需要命名资源,请为其命名,或选择应用于所有(基本上删除名称)。

    由于我喜欢展示和讲述,这里有一些图片:

    然后继续并根据需要进行更改:)

    模板代码:

    <Style TargetType="ListBoxItem">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    【讨论】:

    • 谢谢,这个解决方案对我也有好处:)。我会为下一个列表框尝试它。有图片的好帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多