更改列表框中所有ListBoxItem的样式(Windows Phone 8) [英] Change style for all ListBoxItem in a ListBox (Windows phone 8)

查看:106
本文介绍了更改列表框中所有ListBoxItem的样式(Windows Phone 8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些Listboxitem的Listbox,我想更改所有项目的样式.我知道,可以在资源中创建样式并将此样式绑定到每个项目,但是有可能做到这一点那么容易(没有绑定)吗?有ListBox.ItemTemplate吗?

I have a Listbox with some Listboxitem and I would like to change the style of all items. I know, it's possible to create a style in resources and bind this style to each item but maybe is there a possibility to do it so easier (without binding)? With ListBox.ItemTemplate?

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

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

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

实际上,我的目标是为每一项添加保证金底部15. (在项目之间添加空格)

In fact, my objective is to add Margin bottom 15 for each item. (add a space between items)

推荐答案

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

Note: This works for WPF; not sure if it will work for Windows Phone also.

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

You can create a style using TargetType. This would affect all ListBoxItems for this specific ListBox:

<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>

如果要为所有 ListBoxes设置ListBoxItem的样式,则可以在适当的父级指定样式.

If you want to style ListBoxItems for all ListBoxes, you can specify a style at the appropriate parent level.

例如以下将样式应用于此网格下所有ListBox的所有ListBoxItem.

E.g. following applies style for all ListBoxItems of all ListBoxes under this Grid.

<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>

这篇关于更改列表框中所有ListBoxItem的样式(Windows Phone 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆