首页 > 解决方案 > UWP:如何在我的视图模型中获取轮播控件中使用的图像大小?

问题描述

我正在使用显示不同图像的轮播控件,我希望能够在我的视图模型中获取轮播中使用的图像的大小以进行一些计算。我该怎么做呢 ?

主页.xaml

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d">

 <Grid>
<Border Margin="0">
  <controls:Carousel x:Name="CarouselControl"
              InvertPositive="True"
              ItemDepth="300"
              ItemMargin="340"
              ItemRotationX="0"
              ItemRotationY="45"
              ItemRotationZ ="0"
              Orientation="Horizontal"
              SelectedIndex="4">
    <controls:Carousel.EasingFunction>
      <CubicEase EasingMode="EaseOut" />
    </controls:Carousel.EasingFunction>
    <controls:Carousel.ItemTemplate>
      <DataTemplate>
        <Image 
              x:Name ="Images"
              Width="200"
              Height="200"
              VerticalAlignment="Bottom"
              Source="{Binding Thumbnail}"
              Stretch="Uniform" />
      </DataTemplate>
    </controls:Carousel.ItemTemplate>
  </controls:Carousel>
</Border>

如何在此 xaml 页面的视图模型中访问 x:Name="Images" 中的图像大小?

标签: uwpuwp-xaml

解决方案


您应该将 的 绑定ItemsSourceCarousel视图模型的集合属性,该属性返回表示要在视图中显示的图像的项目。

然后,您可以将视图中元素的WidthHeight属性绑定Image到该对象的相应源属性,就像您当前似乎将Source目标属性绑定到Thumbnail源属性一样。

因此,将Thumbnail属性移动到表示图像或项目的新类,然后将Width属性添加Height到同一类并在 XAML 标记中绑定到它们。


推荐阅读