Windows Phone列表框ItemTemplate绑定到listBoxItem

FendouHui

我的ListBox的ItemTemplate中有一个Button,我想将其可见性绑定到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模式。

有人有什么建议吗?

ngvtotoan

使用可以使用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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

最后一个列表框项目隐藏在Windows Phone 8.1 RT中的命令栏后面

将列表框绑定到ObservableCollection

列表框绑定到数据源(SQL Server数据库)后为空:C#,Windows窗体

数据绑定到int列表“ Windows Phone 8”

排序列表框(Windows Phone)

创建与每个列表框项一起显示的“自定义菜单” Windows Phone

如何从Windows Phone的列表框中选择一个项目?

从“保留”事件中的“ Windows Phone”列表框中获取选定的项目

从sqlite数据库中选择数据并将值绑定到Windows Phone 8应用中的列表框项

文本块下方的数据绑定列表框未在Windows Phone中滚动

用于刷新列表框Windows Phone中的项目的特定UI

如何在Windows Phone 8中制作不可滚动的列表框

如何滚动到Windows Phone 7列表框中的选定项目

为什么比Windows Phone 8中的列表框更喜欢LongListSelector

Windows Phone 8中未显示列表框数据和图像

什么控件用作Windows Phone 8上的内联列表框?

需要在Windows Phone 8中创建带有动画的列表框删除项

如何在Windows Phone绑定中从列表框获取值?

如何在Windows Phone 8中使用没有列表框的xml文件?

使用C#从Windows Phone的列表框中删除所选项目

从Windows Phone 8的列表框中访问特定的文本块

列表框中的Windows Phone进度栏显示随机值

如何在Windows Phone 7的列表框中使用Web浏览器?

由于其保护级别,无法访问列表框Windows Phone 8.1中存在错误

数据绑定SQLIte到列表框(包含三个控件)Windows Phone 8

长列表选择器中的列表框命令未触发-Windows Phone 8-MVVM

列表框数据绑定Windows Phone

如何将组合框数据源绑定到Windows窗体中的窗体/列表框?

多选列表框项返回 system.windows.controls.listboxitem: 与 listboxitem 值