我需要爲ItemsControl
元素中存在的所有TextBlock
和TextBox
定義全局樣式。如何將樣式應用於所有ItemsControl元素中存在的所有TextBlock和TextBox元素?
我需要所有的TextBlock
元素的寬度爲100左對齊,並且TextBox
元素的寬度爲50並右對齊。
我該如何做到這一點?
我需要爲ItemsControl
元素中存在的所有TextBlock
和TextBox
定義全局樣式。如何將樣式應用於所有ItemsControl元素中存在的所有TextBlock和TextBox元素?
我需要所有的TextBlock
元素的寬度爲100左對齊,並且TextBox
元素的寬度爲50並右對齊。
我該如何做到這一點?
我不明白你的「內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。
你可以聲明樣式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>