[英]List Box item container style , Data Template with Convertor
我有列表框,其中嵌入了數據模板和轉換器。
<DataTemplate x:Key="DataTemplate">
<Border x:Name="ListItemBorder"
Margin="0,2,0,0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Background="{Binding Converter={StaticResource BackgroundConvertor}}">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock...
<Image ...
</Grid>
</Border>
</DataTemplate>
該轉換器BackgroundConvertor
在上面的代碼會在列表框中添加綠色作為背景2項中4項。
當我們點擊項目時,我也將樣式應用於列表框。
<Style x:Key="ListItemSelectorStyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Padding" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property ="Foreground" Value="Black" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="ListBoxItem" Background="{TemplateBinding Background}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListItemBorder" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="#c9ebf2" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="ListItemBorder"
BorderBrush="Transparent" Background="#e3e8f0">
<ContentControl x:Name="ContentContainer"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
當我單擊列表項時,樣式根本沒有應用。樣式會將背景設置為所選項。
我在這里做錯了什么?
我正在使用Windows Phone 8應用程序。
您必須修改您的VisualState代碼:
Storyboard.TargetProperty="{TemplateBinding Background}"
使用綁定和轉換器時,必須設置Path屬性:
Background="{Binding Converter={StaticResource BackgroundConvertor}, Path=PROPERTY}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.