2012-04-18 91 views

回答

2

我不明白你的“内ItemsControl的元素存在,”但如果你谈论你的ItemsTemplate它应该像这样工作

<Style x:Key="myTextBoxStyle"> 
    <Setter Property="Width" Value="50"/> 
    <Setter Property="HorizontalAlignment" Value="Right"/> 
</Style> 

<Style x:Key="myTextBlockStyle"> 
    <Setter Property="Width" Value="100"/> 
    <Setter Property="HorizontalAlignment" Value="Left"/> 
</Style> 

<ItemsControl> 
    <ItemsControl.ItemsTemplate> 
     <DataTemplate> 
      <Grid> 
       <TextBlock Style="{StaticResource myTextBlockStyle}"/> 
       <TextBox Style="{StaticResource myTextBoxStyle}"/> 
      <Grid> 
     <DataTemplate> 
    </ItemsControl.ItemsTemplate> 
</ItemsControl> 

这将显示与使用文本框在ItemsControl中的所有项目你的myTextBoxStyle和你的textblock与myTextBlockStyle。

2

你可以声明样式ItemsControl.Resources内部以及

 <ItemsControl ItemsSource="{Binding Persons}"> 
     <ItemsControl.Resources> 
      <Style x:Key="TxtBlk1" TargetType="{x:Type TextBlock}"> 
       <Setter Property="Foreground" Value="red"/> 
       <Setter Property="FontSize" Value="56"/> 
       <Setter Property="HorizontalAlignment" Value="Right"/> 
      </Style> 

     </ItemsControl.Resources> 
     <ItemsControl.ItemTemplate> 

      <DataTemplate> 
       <TextBlock Text="{Binding Name}" Style="{StaticResource TxtBlk1}"></TextBlock> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
相关问题