UWP:将控件的宽度和高度设置为百分比值

时间:2015-09-12 11:28:24

标签: xaml uwp

我正在尝试使用StackPanel控件来显示一些概述选项。当MinWindowWidth大于768px时,我需要使用窗口宽度的20%来控制此控件。当在移动设备上运行时,我需要控件来填充屏幕的宽度。

我按照question中的建议尝试设置百分比值。但是,我收到错误 - “*字符串无法转换为长度”。

这是我的XAML -

        <StackPanel x:Name="OverviewStack">
            <RelativePanel>
                <StackPanel Margin="10" x:Name="User" Orientation="Horizontal">
                    <Ellipse Margin="5" Width="50" Height="50">
                        <Ellipse.Fill>
                            <ImageBrush>
                                <ImageBrush.ImageSource>
                                    <BitmapImage UriSource="Assets/insp.jpg" />
                                </ImageBrush.ImageSource>
                            </ImageBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <TextBlock Margin="5" VerticalAlignment="Center" Text="Ajay Kelkar" />
                </StackPanel>
                <StackPanel Margin="10" x:Name="Options" Orientation="Vertical">
                    <TextBlock Margin="5" Text="Sample text" />
                </StackPanel>
            </RelativePanel>
        </StackPanel>

UWP中无法实现百分比值吗?或者我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要确定当前所在的平台,并相应地设置值。 这听起来像一个转换器。 由于您只有一个UWP项目,您需要在转换器中检查您所在的平台。这样的信息可以这样获得:

if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile") { //Act accordingly }

所以我认为你想做这样的事情:

public object Convert(object value, Type targetType, object parameter, string language)
    {
        // bind the width as the value the converter is set upon
        var width = double.Parse(value.ToString());
        if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile")
        {
            return width;
        }

        // You weren't clear exactly regarding the 768px thing so act accordingly here
        return width * 0.2d;
    }

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