0
在ItemTemplate
用於ItemsControl
我有在頁腳我有幾個按鈕在其上我已增加了幾個ContentControl
S(報頭和報尾)的Xaml樣式不應用於所有按鈕
一種風格,但是內容屬性由樣式設置僅在ItemsControl
的第一個項目,而不是所有的人都上的按鈕應用
ItemsControl的XAML
<ItemsControl ItemsSource="{Binding Posts}" ScrollViewer.VerticalScrollBarVisibility="Auto"
Margin="20">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Background="{StaticResource PostBackground}" CornerRadius="5" Margin="{StaticResource PostMargin}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" Style="{StaticResource PostHeader}"/>
<ContentControl Grid.Row="2" Style="{StaticResource PostFooter}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
風格爲後軀
<Style TargetType="ContentControl" x:Key="PostFooter">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Margin="5 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" HorizontalAlignment="Left">
<Run Text="{Binding Path=NotesCount, Converter={StaticResource NumberToReadableConverter}}"/>
<Run Text="notes"/>
</TextBlock>
<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Style="{StaticResource ReblogButton}" BorderThickness="2" BorderBrush="black"/>
<Button Style="{StaticResource LikeButton}" BorderThickness="2" BorderBrush="black"/>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
和樣式的按鈕
<Style TargetType="Button" x:Key="LikeButton">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Content">
<Setter.Value>
<Grid>
<TextBlock Text="" FontFamily="Segoe MDL2 Assets"/>
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" Foreground="Red" Visibility="Collapsed">
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding Liked}" Value="True">
<core:ChangePropertyAction PropertyName="Visibility" Value="Visible" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</TextBlock>
</Grid>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Button" x:Key="ReblogButton">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Content">
<Setter.Value>
<SymbolIcon Symbol="Switch"/>
</Setter.Value>
</Setter>
</Style>
設置控制的內容一個將被多個控件使用的樣式只在類型類似於字符串或int類型時才起作用。我猜''SymbolIcon Symbol =「Switch」/>'是一個控件本身。如果是這樣的話,那麼你實際上並沒有多次實例化對象,所以只有一個控件實際獲取內容,因爲控件只能有一個直接父對象。 – Meloviz
@Meloviz我剛剛刪除了symbolicon,並用純文本替換它,似乎在每個按鈕中都出現了,但我仍然希望將這些符號放在按鈕中,如果我將按鈕和內容設置爲 ,它會起作用在用戶控件中使用setter並將其放入頁腳? – grabthefish