我需要为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>