在网格视图中拉伸用户控件

时间:2016-03-01 02:21:53

标签: c# gridview user-controls win-universal-app

我在一个uwp app中的GridView中有一个usercontrol。我希望在Gridview ItemsPanel中扩展用户控件。我试图将用户控件的HorizontolAlignment设置为Stretch,但没有成功。有人知道解决方案吗?

这里是usercontrol的xaml

<UserControl
x:Class="LessonsBuild.Controls.TextItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:LessonsBuild.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="614"
HorizontalAlignment="Stretch" 

Margin="5"
Height="Auto">

<Grid Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent },Path=Width}"
      Height="Auto"  >
    <Rectangle x:Name="DropShadow" 
               Margin="5,5,0,0"
               Fill="{StaticResource DropShadowBrush }"/>

    <Grid x:Name="Root" 
          Width ="{Binding RelativeSource={RelativeSource Mode=TemplatedParent },Path=Width}"
          Height="Auto"  
          Margin="0,0,3,3"
          Background="White" >
        <Grid.RowDefinitions >
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>    

            <!--#region Header-->
        <Grid Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent },Path=Width}"
              HorizontalAlignment="Stretch"  >
            <Grid.ColumnDefinitions >
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <TextBlock x:Name="txtTitel"
                       Text="{x:Bind CurrentModel.Title}"
                       FontSize="30"
                       VerticalAlignment="Top"  />
            <StackPanel x:Name="controls"
                        Grid.Column="1"
                        Orientation="Horizontal" >

                <AppBarButton   x:Name="btnBold"
                              Icon="Bold" 
                              Click="btnBold_Click" />

                <AppBarButton x:Name="btnItalic"
                              Icon="Italic"  
                              Click="btnItalic_Click"  />

                <AppBarButton x:Name="btnUnderline"
                              Icon="Underline" 
                              Click="btnUnderline_Click"  />

                <AppBarButton x:Name="btnBullet"
                              Icon="Bullets" 
                              Click="btnBullet_Click"  />


            </StackPanel>

            <AppBarButton x:Name="btnDelete"
                          Grid.Column="2"
                          HorizontalAlignment="Right" 
                          Icon="Delete" 
                          Click="btnDelete_Click" 
                />


        </Grid>
            <!--#endregion-->

        <!--#region  Content-->
        <RichEditBox x:Name="txtBox"
            Grid.Row="1" BorderThickness="0,1"
                     TextChanged="txtBox_TextChanged"
                     >

        </RichEditBox>

        <!--#endregion-->
    </Grid>
</Grid>

继承GridView s xaml

<GridView Grid.Row="1"
              Margin="30"
              ItemsSource="{Binding TextControlList}" SelectionMode="None" 
              HorizontalContentAlignment="Stretch"  />

enter image description here

2 个答案:

答案 0 :(得分:2)

要调整它,请在' ItemContainerStyle '(不在gridview本身内)中将Horizo​​ntalContentAlignment项修改为Stretch。 在list / gridview控件中,您可以通过此ItemContainerStyle修改项目放置的方式。

<GridView x:Name="yourGridView" .... >
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </GridView.ItemContainerStyle>
</GridView>

答案 1 :(得分:0)

尝试使用ListView而不是GridView