XAML UWP -当指针位于ListViewItem上时,ListView显示按钮

r8uurelv  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(155)

有一个ListView的项目包含可见性设置为Collapsed的Button。当指针经过包含该按钮的ListViewItem时,如何使按钮可见?可以使用VisualStateManager实现吗?
我试过下面的方法,但到目前为止还没有成功。可以只使用xaml而不使用C#吗?

<ListView x:Name="Items" Width="500" Height="700">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OkButtom" Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="400"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Some Text" Grid.Column="0"/>
                            <Button x:Name="OkButtom" Visibility="Collapsed" Content="OK" Grid.Column="1"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

字符串

68de4m5k

68de4m5k1#

VisualStateManager不会以这种方式工作。
有几种方法可以实现它,下面是使用XAML Behaviors的方法。从nuget添加Microsoft.Toolkit.Uwp.UI.Behaviors,然后为PointerEnteredPointerExited事件添加EventTriggerBehaviors,以便它们将调用ChangePropertyAction来更改Button的可见性。

<ListView.ItemTemplate>
    <DataTemplate>
        <Border xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
                xmlns:Interactivity="using:Microsoft.Xaml.Interactivity">
            <Grid Width="500" Background="Transparent">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="400"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"
                           Text="Some Text"/>
                <Button Grid.Column="1"
                        x:Name="OkButtom"
                        Content="OK"
                        Visibility="Collapsed"/>

                <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior EventName="PointerEntered">
                        <Core:ChangePropertyAction TargetObject="{Binding ElementName=OkButtom}"
                                                   PropertyName="Visibility" Value="Visible"/>
                    </Core:EventTriggerBehavior>
                    <Core:EventTriggerBehavior EventName="PointerExited">
                        <Core:ChangePropertyAction TargetObject="{Binding ElementName=OkButtom}"
                                                   PropertyName="Visibility" Value="Collapsed"/>
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </Grid>
        </Border>
    </DataTemplate>
</ListView.ItemTemplate>

字符串

相关问题