1

我有一个带有一些 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。(在项目之间添加一个空格)

4

2 回答 2

5

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

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

<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 下所有 ListBoxes 的所有 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>
于 2013-04-16T14:08:03.720 回答
4

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

如果您想设置数据显示方式的样式,那么我建议您创建一个 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(确保设置DataContext 也是如此)

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

这是创建完整模板以覆盖 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>
于 2013-04-16T14:49:01.007 回答