更改ListBox中所有ListBoxItem的样式(Windows Phone 8)

Vol*_*kan 1 c# xaml listbox listboxitem windows-phone-8

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

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

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

                        <ListBoxItem x:Name="ItemEmail">
                                ....
                        </ListBoxItem>
              </Listbox>
Run Code Online (Sandbox Code Playgroud)

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

pub*_*cgk 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>
Run Code Online (Sandbox Code Playgroud)

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

例如,以下为此Grid下的所有ListBox的所有ListBoxItem应用样式.

<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>
Run Code Online (Sandbox Code Playgroud)