开发者

WPF UserControl is not filling parent container when bound at runtime

I have a window that has a StackPanel, and the StackPanel has a ContentControl, which gets a UserControl bound to it at run time.

(In MainWindow.xaml)

<StackPanel Margin="6,14,5,6" Grid.Row="1">
  <ContentControl Name="WindowContent" Content="{Binding}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
</StackPanel>

(In MainWindow.xaml.cs)

WindowContent.Content = new MainWindowView();

I want the UserControl (and it's children) to fill up the space in the StackPanel.

I have checked that all of the Heights and Widths are set to Auto, and Horizontal/VerticalAlignments are set to Stretch, and Horizontal/VerticalContentAlignments are also set to Stretch.

Is there something I'm m开发者_如何学编程issing? This seems like a silly question, but I can't get this to work!

Thanks


The StackPanel container always sizes to its content's minimum size. I believe you want to use a Grid rather than a StackPanel; Grid will attempt to use all available space.

<Grid Margin="6,14,5,6" Grid.Row="1">
   <ContentControl Name="WindowContent" Content="{Binding}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
</Grid> 

Edit: If you want the same kind of stacking functionality in a Grid, just do something like this:

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

That would make 2 minimally sized rows (like a StackPanel) and then a row that took up all the rest of available space.


I found that using a UniformGrid was a more satisfactory solution to this as it allows a dynamic range of items in the container without having to using a specific set of dimensions known in advance. It also works better for large numbers of items. Code snippet where I used this as a solution:

<Grid Name="AmericanFlag" Opacity="{Binding ElementName=AmericanFlagOpacity, Path=Value}">
      <Grid.Resources>
        <Color x:Key="OldGloryRed">#B22234</Color>
        <Color x:Key="OldGloryWhite">#FFFFFF</Color>
        <Color x:Key="OldGloryBlue">#3C3B6E</Color>
        <Style x:Key="RedStripe" TargetType="Border">
          <Setter Property="Background">
            <Setter.Value>
              <SolidColorBrush Color="{StaticResource OldGloryRed}"/>
            </Setter.Value>
          </Setter>
        </Style>
        <Style x:Key="WhiteStripe" TargetType="Border">
          <Setter Property="Background">
            <Setter.Value>
              <SolidColorBrush Color="{StaticResource OldGloryWhite}"/>
            </Setter.Value>
          </Setter>
        </Style>
        <Style x:Key="Star" TargetType="Path">
          <Setter Property="Fill">
            <Setter.Value>
              <SolidColorBrush Color="{StaticResource OldGloryWhite}"/>
            </Setter.Value>
          </Setter>
          <Setter Property="Stretch" Value="Uniform"/>
          <Setter Property="Data" Value="M 9,0 L 7,6 L 0,6 L 6,11 L 4,17 L 9,12 L 14,17 L 12,11 L 18,6 L 11,6 L 9,0"/>
        </Style>
      </Grid.Resources>
      <Grid.RowDefinitions>
        <RowDefinition Height="7*"/>
        <RowDefinition Height="6*"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="3*"/>
      </Grid.ColumnDefinitions>
      <UniformGrid
        Grid.ColumnSpan="2"
        Grid.RowSpan="2"
        Columns="1"
        Rows="13">
        <Border Style="{StaticResource RedStripe}"/>
        <Border Style="{StaticResource WhiteStripe}"/>
        <Border Style="{StaticResource RedStripe}"/>
        <Border Style="{StaticResource WhiteStripe}"/>
        <Border Style="{StaticResource RedStripe}"/>
        <Border Style="{StaticResource WhiteStripe}"/>
        <Border Style="{StaticResource RedStripe}"/>
        <Border Style="{StaticResource WhiteStripe}"/>
        <Border Style="{StaticResource RedStripe}"/>
        <Border Style="{StaticResource WhiteStripe}"/>
        <Border Style="{StaticResource RedStripe}"/>
        <Border Style="{StaticResource WhiteStripe}"/>
        <Border Style="{StaticResource RedStripe}"/>
      </UniformGrid>
      <Border>
        <Border.Background>
          <SolidColorBrush Color="{StaticResource OldGloryBlue}"/>
        </Border.Background>
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="18*"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="22*"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
          <UniformGrid
            Grid.Column="1"
            Grid.Row="1"
            Columns="1"
            Rows="9">
            <UniformGrid Columns="6" Rows="1">
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
            </UniformGrid>
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10*"/>
                <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <UniformGrid Grid.Column="1" Columns="5" Rows="1">
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
              </UniformGrid>
            </Grid>
            <UniformGrid Columns="6" Rows="1">
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
            </UniformGrid>
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10*"/>
                <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <UniformGrid Grid.Column="1" Columns="5" Rows="1">
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
              </UniformGrid>
            </Grid>
            <UniformGrid Columns="6" Rows="1">
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
            </UniformGrid>
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10*"/>
                <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <UniformGrid Grid.Column="1" Columns="5" Rows="1">
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
              </UniformGrid>
            </Grid>
            <UniformGrid Columns="6" Rows="1">
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
            </UniformGrid>
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10*"/>
                <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
              <UniformGrid Grid.Column="1" Columns="5" Rows="1">
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
                <Path Style="{StaticResource Star}"/>
              </UniformGrid>
            </Grid>
            <UniformGrid Columns="6" Rows="1">
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
              <Path Style="{StaticResource Star}"/>
            </UniformGrid>
          </UniformGrid>
        </Grid>
      </Border>
    </Grid>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜