Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在UWP应用程序中的元素上添加鼠标悬停和按下/点击效果_C#_Xaml_Uwp_Windows Store_Uwp Xaml - Fatal编程技术网

C# 在UWP应用程序中的元素上添加鼠标悬停和按下/点击效果

C# 在UWP应用程序中的元素上添加鼠标悬停和按下/点击效果,c#,xaml,uwp,windows-store,uwp-xaml,C#,Xaml,Uwp,Windows Store,Uwp Xaml,我目前正在开发一个应用程序来显示科尔特里克(比利时的一个城市)的所有停车位。目前情况是这样的: 设计 我的问题是:例如,如何在鼠标上方或单击时更改元素的颜色。我想在XAML中实现这一点,这就是我现在拥有的代码 代码 MainPage.xaml style.xaml(外部xaml文件) #FFB3B6F2 #FF5A58D9 #FFF2F2F2 我尝试了style.triggers,但这仅在WPF应用程序中可能,在UWP应用程序中不可能。我也读过很多关于VisualState的文章

我目前正在开发一个应用程序来显示科尔特里克(比利时的一个城市)的所有停车位。目前情况是这样的:

设计
我的问题是:例如,如何在鼠标上方或单击时更改元素的颜色。我想在XAML中实现这一点,这就是我现在拥有的代码

代码 MainPage.xaml

style.xaml(外部xaml文件)

#FFB3B6F2
#FF5A58D9
#FFF2F2F2

我尝试了style.triggers,但这仅在WPF应用程序中可能,在UWP应用程序中不可能。我也读过很多关于VisualState的文章,但我不知道如何使用它,也不知道这是否是实现这种效果的最佳方法


提前感谢

您可能应该使用ListView来显示此数据,而不是ItemsControl(除非您有充分的理由这样做)。ListView扩展了ItemsControl,并为其添加了许多有用的功能,例如:

  • 单个/多个项目选择
  • ItemClick事件
  • 每个项目容器都是一个ListViewItem控件,它有自己的功能,如可视状态和复选框,ListViewItem的显示由ListViewItemPresenter管理,ListViewItemPresenter可以以优化的方式提供这些功能
  • 内置滚动查看器
  • 数据和UI虚拟化。当您有100个项目时,UI虚拟化是一个很大的优势
  • 容易接近。支持标签对焦
  • 可能更多
ItemsControl通常不用于希望与项目交互的情况(例如,通过单击/点击)

默认情况下,ListView有自己的样式,可以很容易地将其覆盖以匹配您已有的样式

如果只希望为每个视觉状态设置ListViewItem背景/前景的样式,则可以替代这些样式:


您可能应该使用ListView而不是ItemsControl来显示此数据(除非您有充分的理由这样做)。ListView扩展了ItemsControl,并为其添加了许多有用的功能,例如:

  • 单个/多个项目选择
  • ItemClick事件
  • 每个项目容器都是一个ListViewItem控件,它有自己的功能,如可视状态和复选框,ListViewItem的显示由ListViewItemPresenter管理,ListViewItemPresenter可以以优化的方式提供这些功能
  • 内置滚动查看器
  • 数据和UI虚拟化。当您有100个项目时,UI虚拟化是一个很大的优势
  • 容易接近。支持标签对焦
  • 可能更多
ItemsControl通常不用于希望与项目交互的情况(例如,通过单击/点击)

默认情况下,ListView有自己的样式,可以很容易地将其覆盖以匹配您已有的样式

如果只希望为每个视觉状态设置ListViewItem背景/前景的样式,则可以替代这些样式:



您能解释一下为什么这应该是一个ListView而不是ItemsControl吗?仍然可以在ListView中使用VariableSizedWrapGrid吗。我需要这个来在更大的屏幕上显示它们,我已经更新了问题中的代码。我还没有实现悬停和单击效果,但在这段代码中,我的应用程序崩溃。你需要使用而不是VariableSizedWrapGrid。如果你的项目主要以网格方式显示,你甚至可能想使用GridView而不是ListView。你能解释一下为什么这应该是ListView而不是ItemsControl吗?它仍然存在吗可以在ListView中使用VariableSizedWrapGrid。我需要这个来在更大的屏幕上显示它们,我已经更新了问题中的代码。我还没有实现悬停和单击效果,但在这段代码中,我的应用程序崩溃。您需要使用而不是VariableSizedWrapGrid。如果您的项目主要以网格方式显示,您甚至可能希望使用GridView而不是ListView。
<Page
x:Class="ParkingSpots.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ParkingSpots"
xmlns:model="using:ParkingSpots.model"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Maps="using:Windows.UI.Xaml.Controls.Maps"
mc:Ignorable="d">

<Page.Resources>
    <model:ParkingSpot x:Key="spots"/>
</Page.Resources>

<Grid Style="{StaticResource mainGrid}">
    <Grid.RowDefinitions>
        <RowDefinition Height="20"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="Parking spots in Kortrijk"/>
    <ListView ItemsSource="{Binding Source={StaticResource spots}, Path=ParkingSpots}" ItemTemplate="{StaticResource ParkingSpotTemplate}" ItemsPanel="{StaticResource ParkingSpotsTemplate}"/>
</Grid>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ParkingSpots.style"
xmlns:conv="using:ParkingSpots.converter">

<conv:StreetConverter x:Key="StreetConv" />

<Color x:Key="Color1">#FFB3B6F2</Color>
<Color x:Key="Color2">#FF5A58D9</Color>
<Color x:Key="Color3">#FFF2F2F2</Color>

<SolidColorBrush x:Key="Color1Brush" Color="{StaticResource Color1}" />
<SolidColorBrush x:Key="Color2Brush" Color="{StaticResource Color2}" />
<SolidColorBrush x:Key="Color3Brush" Color="{StaticResource Color3}" />

<Style x:Name="mainGrid" TargetType="Grid">
    <Setter Property="Background" Value="{StaticResource Color1Brush}"/>
</Style>

<DataTemplate x:Key="ParkingSpotTemplate">
    <ListViewItem>
        <ListViewItem.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="{StaticResource Color3Brush}" />
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="Margin" Value="8,0,0,0" />
            </Style>
        </ListViewItem.Resources>
        <TextBlock x:Name="ParkingSpotInfo" Grid.Row="0" Grid.Column="0" Text="{Binding Street, Converter={StaticResource StreetConv}}"/>
    </ListViewItem>
</DataTemplate>

<ItemsPanelTemplate x:Key="ParkingSpotsTemplate">
    <VariableSizedWrapGrid x:Name="wrapGrid"></VariableSizedWrapGrid>
</ItemsPanelTemplate>