UWP DataTemplate 中的列未拉伸

nzm*_*ike 3 c# xaml datatemplate mvvm win-universal-app

我正在编写一个 UWP 应用程序来跟踪观看/购买/流式传输等的电视节目,并且我非常疯狂地尝试在 DataTempate 中获取网格列以拉伸它们的宽度,因为 XAML 中似乎存在一个错误,它忽略了 * 宽度定义。我需要 ListView 中的第一列(节目标题)来占用剩余空间(因此列定义 = "*"),虽然它会在 HeaderTemplate 中这样做,但它绝对拒绝在 DataTemplate 中这样做,所以整个网格最终会变得不稳定且不对齐,因为标题列仅使用每行所需的空间。

我的 XAML 如下 - 在 ItemTemplate DataTemplate 模板中,我绑定到一个名为 TVShow 的对象实例,该实例位于我的主视图模型的可观察集合中。(我没有在此处包含 ViewModel 或 TVShow 类定义,因为我知道这纯粹是 XAML 问题)。

到目前为止唯一有效的是在我的 TVShow 类中有一个额外的属性来存储列的正确宽度(通过从网格大小中减去其他三列的宽度(在后面的视图代码中获取),但这会导致整个列表在最初显示后重新格式化,看起来很难看,更不用说糟糕的编程了。

所以我正在寻找关于如何解决这个问题的想法 - 我可以在主视图模型中移动正确列宽的属性,但是如果我绑定到“TVShow”,我该如何绑定到模板中的那个?或者我是否必须从 DataTemplate 中取出内容并放入 UserControl?我已经在如此简单的事情上浪费了太多时间——这个错误似乎自 WPF 以来就存在,为什么 MS 从来没有解决过这个问题——非常令人沮丧。

<HubSection Name="hsShows" Width="{Binding HubSectionWidth}" MinWidth="430" MaxWidth="640" 
            VerticalAlignment="Top" HorizontalAlignment="Stretch" Background="{StaticResource Dark}" >

    <HubSection.Header>
        <TextBlock Text="Shows" TextLineBounds="TrimToBaseline" OpticalMarginAlignment="TrimSideBearings" 
                                   FontSize="24" Foreground="{StaticResource Light}"/>
    </HubSection.Header>

    <DataTemplate x:DataType="local:MainPage">

        <ListView Name="lvwShows" 
                                  Width="{Binding HubSectionGridWidth}"
                                  Grid.Row="0" 
                                  Foreground="{StaticResource Light}"
                                  Background="{StaticResource Dark}"
                                  Margin="-14,20,0,0" 
                                  Loaded="lvwShows_Loaded"
                                  ItemsSource="{Binding AllShows}"                             
                                  HorizontalAlignment="Stretch"
                                  HorizontalContentAlignment="Stretch" 
                                  IsSwipeEnabled="True"
                                  IsItemClickEnabled="True"
                                  SelectedItem="{Binding SelectedTVShow, Mode=TwoWay}"                                
                                  SelectionMode="Single"                              
                                  ScrollViewer.VerticalScrollMode="Enabled"
                                  ScrollViewer.VerticalScrollBarVisibility="Auto">

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.HeaderTemplate>
                <DataTemplate>
                    <Grid Width="{Binding HubSectionGridWidth}" Height="Auto" Background="DarkGreen" Margin="15,5,5,5" HorizontalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="80"/>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="80"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="Title" FontSize="16"  FontWeight="Bold" Foreground="{StaticResource Bright}" 
                                                   VerticalAlignment="Bottom" HorizontalAlignment="Left" 
                                                   Tag="TITLE,ASC" Tapped="ShowsGridHeading_Tapped"/>

                        <TextBlock Grid.Column="1"  Text="Seasons" FontSize="16"  FontWeight="Bold" Foreground="{StaticResource Bright}" 
                                                   VerticalAlignment="Bottom" HorizontalAlignment="Center" 
                                                   Tag="SEASONS,ASC" Tapped="ShowsGridHeading_Tapped"/>

                        <TextBlock Grid.Column="2" Text="Last Watched" FontSize="16"  FontWeight="Bold" Foreground="{StaticResource Bright}" 
                                                   VerticalAlignment="Bottom" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" 
                                                   Tag="WATCHED,ASC" Tapped="ShowsGridHeading_Tapped"/>

                        <TextBlock Grid.Column="3" Text="Last Episode" FontSize="16"  FontWeight="Bold" Foreground="{StaticResource Bright}" 
                                                   VerticalAlignment="Bottom" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" 
                                                   Tag="EPISODE,ASC" Tapped="ShowsGridHeading_Tapped"/>
                    </Grid>
                </DataTemplate>
            </ListView.HeaderTemplate>

            <ListView.ItemTemplate>
                <DataTemplate x:DataType="model:TVShow">
                    <Grid Height="Auto"  MinWidth="410" MaxWidth="640"  Background="Blue" HorizontalAlignment="Stretch" RightTapped="ShowsList_RightTapped">

                        <FlyoutBase.AttachedFlyout>
                            <MenuFlyout Placement="Bottom">
                                <MenuFlyoutItem x:Name="UpdateButton" Text="Update from TVMaze" Click="FlyoutUpdateButton_Click"/>
                                <MenuFlyoutItem x:Name="RefreshButton" Text="Refresh" Click="FlyoutRefreshButton_Click"/>
                                <MenuFlyoutItem x:Name="DeleteButton" Text="Delete Show" Click="FlyoutDeleteButton_Click"/>
                            </MenuFlyout>
                        </FlyoutBase.AttachedFlyout>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="80"/>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="80"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{x:Bind Title}"  Foreground="{StaticResource Light}"
                                                   VerticalAlignment="Center" HorizontalAlignment="Stretch" TextWrapping="Wrap" />

                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{x:Bind Seasons}"  Foreground="{StaticResource Light}"
                                                   VerticalAlignment="Center" HorizontalAlignment="Center"/>

                        <TextBlock Grid.Row="0" Grid.Column="2"   Foreground="{StaticResource Light}"
                                                   Text="{x:Bind LastWatchedDate, Mode=OneWay, Converter={StaticResource DateTimeFormatConverter}, ConverterParameter='{}{0:dd/MM/yyy HH\\\\:mm}'}"                            
                                                   VerticalAlignment="Center" HorizontalAlignment="Center"/>

                        <TextBlock Grid.Row="0" Grid.Column="3" Text="{Binding LastWatchedEpisodeRef}"  Foreground="{StaticResource Light}"
                                                   VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </DataTemplate>
</HubSection>
Run Code Online (Sandbox Code Playgroud)

nzm*_*ike 6

好的,所以我最终将这个 XAML 添加到我的 ListViews 中(虽然我知道我可以按照 Grace 的建议去做,但我发现 Blend 使用起来很糟糕)——实际上是 Horizo​​ntalContentAlignment 做到了这一点!

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)