【问题标题】:Set xaml object height as % of other xaml object将 xaml 对象高度设置为其他 xaml 对象的百分比
【发布时间】:2016-01-04 13:46:53
【问题描述】:

在我的 UWP 应用的 DataTemplate 中,我想将矩形的高度设置为图像高度的百分比。

我的代码如下所示。

 <DataTemplate
        x:Key="FlipViewTemplate">
        <Grid>

                <Image
                    x:Name="image"
                    Margin="20"
                    Source="{Binding fullImgUrl}"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center" />
                <Rectangle
                    Height="{Binding 
                   Converter={StaticResource PercentageConverter}, 
                   ElementName=image, 
                   Path=ActualHeight, 
                   ConverterParameter=0.2}"
                    Fill="#FFEA1E1E"
                    VerticalAlignment="Bottom" />

        </Grid>
    </DataTemplate>

我的转换器也是这样的

 public class PercentageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return System.Convert.ToDouble(value) *
              System.Convert.ToDouble(parameter);
    }


    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }


}

我的问题是转换器中应该是图像实际高度的值始终为 0。

【问题讨论】:

    标签: xaml uwp percentage ivalueconverter


    【解决方案1】:

    您已经在使用 Grid,因此您可以简单地定义两行。在下面的示例中,Image 控件跨越两行,即整个 Grid,而 Rectangle 仅位于第二行,占整个高度的 20%。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="4*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Image Grid.RowSpan="2" .../>
        <Rectangle Grid.Row="1" .../>
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2013-03-24
      • 2012-12-01
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多