6

我正在使用 Caliburn Micro 开发 Windows Store App。

在我拥有的ContentControl其中一个页面中,显示UserControl. 在UserControl我有GridView.

我的问题是:如何设置UserControl.Width相同ContentControl.Width
注意: whit set UserControl.Width=Auto- 宽度与GridView.Width

在 page.xaml

<ContentControl x:Name="ActiveItem" />

在 usercontrol.xaml

<UserControl
x:Class="Test.Views.GroupView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Width="Auto" Height="Auto">

    <Grid Margin="0,20">
        <GridView x:Name="Groups" Margin="0" />
    </Grid>
</UserControl>



更新

添加

  VerticalAlignment="Stretch"
  HorizontalAlignment="Stretch"

UserControl并不能解决问题。

4

5 回答 5

13

经过大量的试验和错误后想通了:

<ContentControl Name="MyContent" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">

关键是使用 Horizo​​ntal/Vertical* Content *Alignment 属性(不是 Horizo​​ntal/VerticalAlignment 属性)。

于 2013-07-13T05:26:36.420 回答
2

当出现像您这样的问题时,您应该执行以下操作。

  1. 尝试将ContentControl'Background属性设置为一些令人不安的颜色。例如紫色或粉红色。并在您的例如 Green上设置Background属性。UserControl它将允许您查看您的确切位置ContentControl和位置UserControl。如果你看不到任何绿色,你可以看出它的内容UserControl被拉伸以填充整个UserControl

  2. 尝试将UserControl'sVerticalAlignmentHorizontalAlignmentproperties 设置为Stretch. FrameworkElement.Horizo​​ntalAlignment , VerticalAlignment

    注意:为了让这些工作。您不能UserControl上显式设置WidthHeight

  3. 尝试设置ContentControl'sVerticalContentAlignmentHorizontalContentAlignmentto StretchControl.Horizo​​ntalContentAlignmentVerticalContentAlignment。这些拉伸子元素以填充父元素的分配布局空间。

  4. 如果您仍然看到一些紫色或粉红色,那么又有问题了 :) 您可以查看Margin/Padding MSDN

如果还是乱七八糟。那我不知道我还能怎么帮你。最后可能的解决方案是绑定。而且我不确定它是否有效。

<UserControl
Width="{Binding RelativeSource=
        {RelativeSource FindAncestor,
        AncestorType={x:Type ContentControl}},
        Path=ActualWidth}"
Height="{Binding RelativeSource=
        {RelativeSource FindAncestor,
          AncestorType={x:Type ContentControl}},
          Path=ActualHeight}">
...
</UserControl>

我希望有帮助。我相信你,这可能是一个非常烦人的问题。

于 2013-05-10T12:18:24.727 回答
2

特别是对于@AlexeiMalashkevich,
我使用这样的绑定来解决它:

在根页面中,您有:

 <ContentControl x:Name="ActiveItem"/>

然后添加子页面:

<Page
    Height="{Binding ActualHeight, ElementName=ActiveItem}"
    Width="{Binding ActualWidth, ElementName=ActiveItem}"
    ......
/>

就这样。

于 2013-07-29T06:01:00.343 回答
1

对我来说,这是有效的:

<UserControl
                 ...
                    Height="{Binding ActualHeight,
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}}">

...

</UserControl>

您必须确保您ContentControl具有所需的尺寸。

例如我的ContentControl样子是这样的:它总是填满窗口的整个大小。并且UserControlContentControl动态变化中的大小也是如此。

<Grid>
    <ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding StartViewModel}" Name="ContentArea" />
</Grid>
于 2014-08-20T11:03:30.023 回答
1

您应该能够将 UserControl 的宽度绑定到 ContentControl 的 ActualWidth。

<local:MyUserControl1 Height="50" Width="{Binding ElementName=contentControl, Path=ActualWidth}"/>

这是一些示例代码:

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

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Background="Cyan"/>
        <ContentControl Grid.Column="1" x:Name="contentControl">
            <local:MyUserControl1 Height="50" Width="{Binding ElementName=contentControl, Path=ActualWidth}"/>
        </ContentControl>
    </Grid>
</Page>

这是 MyUserControl1.xaml 代码:

<UserControl
    x:Class="stofUserControlWidth.MyUserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:stofUserControlWidth"
    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="400">

    <Grid Background="Magenta">        
    </Grid>
</UserControl>

希望这可以帮助!

于 2013-05-14T00:54:10.800 回答