windows phone listbox ItemTemplate绑定到listBoxItem

时间:2014-05-07 10:40:37

标签: xaml windows-phone-8 listbox

我在ListBox的ItemTemplate中有一个Button,我想将它的Visibility绑定到ListBox Item IsSelected。因此,选择项目时会显示按钮。

我发现以下代码在WPF上运行正常:

<Button Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1},Path=IsSelected}",Converter={StaticResource BooleanToVisibilityConverter}/>

但是Windows Phone上的RelativeResource不支持FindAncestor模式。

有人有什么建议吗?

1 个答案:

答案 0 :(得分:2)

使用可以使用ListBox的ItemContainerStyle:

           <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Top"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border x:Name="LayoutRoot" Background="{TemplateBinding Background}" Visibility="{Binding Visibility}" >
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualState x:Name="Unselected"/>
                                        <VisualState x:Name="Selected">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="testbutton">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <StackPanel>
                                    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="Center" Margin="{TemplateBinding Padding}" VerticalContentAlignment="Center"/>
                                    <Button x:Name="testbutton" Visibility="Collapsed" Width="200" Height="100" Content="Test button"/>
                                </StackPanel>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

        <ListBox Width="480" Background="DarkGray"  
                ItemContainerStyle="{StaticResource ListBoxItemStyle}" 
                ScrollViewer.VerticalScrollBarVisibility="Disabled"
                ScrollViewer.HorizontalScrollBarVisibility="Hidden">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                        <TextBlock Text="{Binding Header}" TextAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>