XAML UWP Stackpanel大小相对于屏幕大小

时间:2018-03-22 02:23:37

标签: xaml uwp pivot uwp-xaml stackpanel

我正在构建一个显示天气数据的UWP应用程序。目前我正在处理一个在枢轴

中使用的dataTemplate

我无法获得相对于屏幕尺寸(或其父容器)的Stackpanels宽度,并且无法将它们居中,我还希望相对于屏幕尺寸设置每边的边距(因为我,我用html之前放心。)

也可以将图像作为每个堆栈面板的背景,或者在我获取Error: "The property 'VisualTree' is set more than once"时将数据显示在数据旁边?

我已检查过此处可用的所有其他帖子但尚无法找到解决方案

<Page
x:Class="WeatherForecast.WeatherPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WeatherForecast"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
    <DataTemplate x:Name="ListViewDataTemplate"  >

        <Grid HorizontalAlignment="Stretch" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="8*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid BorderBrush="Black" BorderThickness="2" Margin="2" HorizontalAlignment="Stretch" Background="DeepSkyBlue" Grid.Column="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding icon}" Stretch="Fill" Height="100" Width="150" Margin="20" Grid.Column="1"/>

                <StackPanel VerticalAlignment="Center">
                    <StackPanel Orientation="Horizontal"  >
                        <TextBlock HorizontalAlignment="Stretch" FontWeight="Bold" Text="Temperature: " ></TextBlock>
                        <TextBlock  HorizontalAlignment="Stretch" Text="{Binding temp}"></TextBlock>

                    </StackPanel>

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Time : " FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding dtime}"></TextBlock>
                    </StackPanel>

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Description : " FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding desc}"></TextBlock>
                    </StackPanel>

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Humidity : " FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding humidity}"></TextBlock>
                    </StackPanel>

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Windspeed : " FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding windSpeed}"></TextBlock>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </Grid>

    </DataTemplate>
</Page.Resources>

<Grid Background="LightBlue">
    <TextBlock x:Name="cityBox" HorizontalAlignment="Center" FontSize="50"  ></TextBlock>
    <Button
        x:Name="ReturnToMain" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,10" BorderThickness="1,1,1,1" FontSize="48" Background="#33B40C0C" BorderBrush="#FFF50B0B" FontFamily="Bauhaus 93" FontWeight="Bold" AutomationProperties.HelpText="Click to return to main" Click="ReturnToMain_Click" >Return To Main</Button>
    <Pivot x:Name="pvtWeather"  HorizontalContentAlignment="Stretch" Margin="100,100,100,100" Height="500" Background="LightBlue"  Foreground="LightPink"     >

    </Pivot>

</Grid>

    foreach (var day in myForecast.SortedDays)
        {
            var weathers = new ObservableCollection<WeatherController>();
            foreach (var weatherItem in day)
            {
                weathers.Add(weatherItem);
            }

            var pivotItem = new PivotItem
            {
                Header = myForecast.SortedDays[index++][0].dayOfWeek
            };
            ListView listView = new ListView
            {
                ItemsSource = weathers
            };
            pivotItem.Content = listView;
            listView.ItemTemplate = ListViewDataTemplate;
            pvtWeather.Items.Add(pivotItem);


        }

我现在有什么 enter image description here 我想要实现的目标 enter image description here

1 个答案:

答案 0 :(得分:1)

你只需要使用StackPanel进行布局就太过分了。如果你想做一些相对大小调整,你需要使用转换器和一些使用帮助器的绑定,比如UWP社区工具包中的FrameworkExtensions

或者你可以使用网格来利用它们的列大小:

<Pivot>
        <PivotItem Header="Thursday">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="8*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid BorderBrush="Black" BorderThickness="2" Margin="2" HorizontalAlignment="Stretch" Background="DeepSkyBlue" Grid.Column="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="http://unsplash.it/g/100/150" Stretch="Fill" Height="100" Width="150" Margin="20" Grid.Column="1"/>

                    <StackPanel VerticalAlignment="Center">
                        <StackPanel Orientation="Horizontal"  >
                            <TextBlock HorizontalAlignment="Stretch" FontWeight="Bold" Text="Temperature: " ></TextBlock>
                            <TextBlock  HorizontalAlignment="Stretch" Text="{Binding temp}"></TextBlock>

                        </StackPanel>

                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Time : " FontWeight="Bold"></TextBlock>
                            <TextBlock Text="{Binding dtime}"></TextBlock>
                        </StackPanel>

                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Description : " FontWeight="Bold"></TextBlock>
                            <TextBlock Text="{Binding desc}"></TextBlock>
                        </StackPanel>

                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Humidity : " FontWeight="Bold"></TextBlock>
                            <TextBlock Text="{Binding humidity}"></TextBlock>
                        </StackPanel>

                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Windspeed : " FontWeight="Bold"></TextBlock>
                            <TextBlock Text="{Binding windSpeed}"></TextBlock>
                        </StackPanel>
                    </StackPanel>
                </Grid>
            </Grid>
        </PivotItem>
    </Pivot>

上面的例子也将图像放在一边,中间区域占据了枢轴宽度的80%。

如果没有关于您已设置的列表/数据透视组合的更多信息,有助于提供更多帮助。