0

我有一個列表視圖和內容主持人的主/詳細信息頁面。我的問題是,當文本塊的內容小於當前窗口時,內容展示器不會填充父網格的整個剩餘空間。ContentPresenter在Windows 10的uwp應用程序不擴展到全寬

我曾嘗試添加Horizo​​ntalAlign ='Stretch'和VerticalAlign ='Stretch',但沒有任何效果。

下面是該MasterDetailPage.xaml

<Page.Resources> 
    <DataTemplate x:Key="MasterListViewItemTemplate" x:DataType="data:List"> 
     <Grid Margin="0,11,0,13"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
      </Grid.RowDefinitions> 

      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="Auto" /> 
      </Grid.ColumnDefinitions> 

      <TextBlock Text="{x:Bind Title}" Style="{ThemeResource BaseTextBlockStyle}" /> 

      <TextBlock Style="{ThemeResource CaptionTextBlockStyle}" 
       Text="{x:Bind Date}" 
       Grid.Column="1" 
       Margin="12,1,0,0" /> 
     </Grid> 
    </DataTemplate> 

    <DataTemplate x:Key="DetailContentTemplate" x:DataType="data:List"> 
     <ScrollViewer VerticalScrollBarVisibility="Auto"> 
      <StackPanel 
       Orientation="Vertical" 
       Margin="0,9,12,9"> 
       <TextBlock 
       Margin="0,8" 
       Style="{ThemeResource TitleTextBlockStyle}" 
       TextWrapping="WrapWholeWords" 
       Text="{x:Bind Title}"/> 

       <RichTextBlock 
       x:Name="textContent" 
       IsTextSelectionEnabled="True" 
       TextWrapping="WrapWholeWords" 
       common:Html2TextParser.Html="{x:Bind Content}"/> 
      </StackPanel> 
     </ScrollViewer> 
    </DataTemplate> 

    <DataTemplate x:Key="comboListTemplate" x:DataType="data:Combo"> 
     <TextBlock Text="{x:Bind Title}"/> 
    </DataTemplate> 
</Page.Resources> 

<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" UseLayoutRounding="True"> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="AdaptiveStates" CurrentStateChanged="AdaptiveStates_CurrentStateChanged"> 
      <VisualState x:Name="DefaultState"> 
       <VisualState.StateTriggers> 
        <AdaptiveTrigger MinWindowWidth="720" /> 
       </VisualState.StateTriggers> 
      </VisualState> 

      <VisualState x:Name="NarrowState"> 
       <VisualState.StateTriggers> 
        <AdaptiveTrigger MinWindowWidth="0" /> 
       </VisualState.StateTriggers> 

       <VisualState.Setters> 
        <Setter Target="MasterColumn.Width" Value="*" /> 
        <Setter Target="DetailColumn.Width" Value="0" /> 
        <Setter Target="MasterListView.SelectionMode" Value="None" /> 
       </VisualState.Setters> 
      </VisualState> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 

    <Grid.ColumnDefinitions> 
     <ColumnDefinition x:Name="MasterColumn" Width="320" /> 
     <ColumnDefinition x:Name="DetailColumn" Width="*" /> 
    </Grid.ColumnDefinitions> 

    <ComboBox 
     Name="comboList" 
     Margin="2,2,2,2" 
     ItemsSource="{x:Bind comboSource.combos}" 
     LayoutUpdated="comboList_LayoutUpdated" 
     SelectionChanged="comboList_SelectionChanged" 
     ItemTemplate="{StaticResource comboListTemplate}" 
     HorizontalAlignment="Stretch" /> 

    <ListView 
     x:Name="MasterListView" ItemsSource="{x:Bind listSource.lists}" 
     Grid.Row="1" 
     ItemContainerTransitions="{x:Null}" 
     ItemTemplate="{StaticResource MasterListViewItemTemplate}" 
     IsItemClickEnabled="True" 
     ItemClick="MasterListView_ItemClick"> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
      </Style> 
     </ListView.ItemContainerStyle> 
    </ListView> 

    <ContentPresenter 
     x:Name="DetailContentPresenter" 
     Grid.Column="1" 
     Grid.RowSpan="2" 
     BorderThickness="1,0,0,0" 
     BorderBrush="{ThemeResource SystemControlForegroundBaseLowBrush}" 
     Content="{x:Bind MasterListView.SelectedItem, Mode=OneWay}" 
     ContentTemplate="{StaticResource DetailContentTemplate}" 
     HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch" 
     Margin="12,0,0,0"> 
     <ContentPresenter.ContentTransitions> 
      <!-- Empty by default. See MasterListView_ItemClick --> 
      <TransitionCollection /> 
     </ContentPresenter.ContentTransitions> 
    </ContentPresenter> 
</Grid> 

誰能解決這個問題的代碼?這是我的問題的截圖。

ScrollViewer not filling width

+0

我測試了你編輯它的代碼[this](http://i.stack.imgur.com/1hmIh.jpg)。它似乎有效。 –

+0

@Jayden Gu也是滾動查看器工作?在richtextblock中添加的run元素是否也有水平對齊設置爲'Stretch'?如果是這樣,我該如何添加它? –

+0

沒有運行..它是塊*做塊需要水平對齊設置伸展嗎? –

回答

0

在爲ContentPresenter和主網格本身設置了不同的顏色後,我發現即使我的主網格「LayoutRoot」沒有擴展到全寬!

原因是我正在使用SplitView,並且我在SpiltView的內容元素中導航了我的MasterDetailPage.xaml。我只是設置Horizo​​ntalAlign ='Stretch'SplitView它解決了我的問題!

0

你可以嘗試把你的contentpresenter視框裏面。 ViewBox旨在伸展和縮放單個孩子以填充可見空間。

Documentation here

我希望可以幫助你:)

+0

它沒有工作..雖然視窗確實拉伸的內容,以適應空間,內容被縮小到這樣做,因此滾動查看器從來沒有出現 –

0

我測試你的XAML和我無法重現你的問題請看截圖 我藍色的空間就是你contentPresenter,橙色空間是ScrollViewer中最後是你的文本塊與虛擬文本。 enter image description here

+0

感謝您檢查了這一點..我似乎已經犯了一個錯誤碼。它是一個RichTextBlock,而不是一個簡單的TextBlock。我已經改變了代碼來反映這個..並且還添加了錯誤的快照以供參考 –

+0

好的讓我檢查 – RicardoPons

相關問題