【问题标题】:Is it possible to make a grid control act as a container control是否可以使网格控件充当容器控件
【发布时间】:2011-08-11 04:15:29
【问题描述】:

我正在使用 Silverlight,并最终使用 Silverlight for the Phone。

我想要做的是创建一个包含子控件的带边框的容器。子控件在网格控件范围之外时不可见。

这可能吗?我知道我可以从路径创建剪辑,但这是唯一的方法。

我确实使用了一个滚动容器,它似乎可以工作......有点。

这是 xaml。我期望的是应用程序运行时第二个按钮不可见。

<Grid x:Name="LayoutRoot" Background="White" >
    <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="197" Width="241" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment">
        <Button Content="Button" Margin="25,42,101,81"/>
        <Button Content="Button" Height="76" Margin="25,0,63,-83" VerticalAlignment="Bottom"/>
    </Grid>
</Grid>

【问题讨论】:

  • 不确定您在问什么——通常,除非您使用 TranslateTransform 之类的东西,否则子元素在 Grid 元素之外是不可见的。
  • 让我感到困惑的是包含的网格边界似乎除了充当参考点之外什么都不做。包含的网格似乎与根的大小相同(实际上)
  • 我仍然不明白您的问题,但要回答您的最后一条评论,Grid 始终采用其容器的大小,因此包含的 Grid 将与 LayoutRoot Grid 的大小完全相同。跨度>
  • 我添加了上面的 xaml。第一个按钮位于网格的预期可见部分。第二个按钮在网格之外,但在布局根中仍然可见。我只想知道如何让第二个包含的网格像一个窗口一样。
  • 好的,如果您不希望显示第二个按钮,请使用 Ross 在下面的答案,因为您的 Margin 就像 TranslateTransform 一样,并将按钮按到网格之外。不知道你为什么要这样做;要隐藏 Button,最好将其 Visibility 设置为 Collapsed。

标签: silverlight xaml windows-phone-7


【解决方案1】:

使用UIElement.ClipToBounds

http://msdn.microsoft.com/en-us/library/system.windows.uielement.cliptobounds.aspx

<Grid ClipToBounds="True">

    ...

</Grid>

【讨论】:

  • 不,它也适用于 Grid。试试上面的 David 教练的 Xaml。
  • 不存在名为 ClipToBounds 的属性。有一个剪辑。
  • 它确实存在于所有 UIElement 派生对象:msdn.microsoft.com/en-us/library/…
  • 它不在 Silverlight 中,只是在 WPF 中。有没有另一种方法可以做到这一点。我不喜欢使用网格。我只想创建父容器增长并显示其他子控件的效果。
【解决方案2】:

因为您似乎要问的是Grid 控件在默认情况下是如何工作的,例如:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <DockPanel Margin="50" Background="Azure" > 
    <Grid>
      <TextBlock>The only part of this control that gets rendered is that which is inside the bounds of the Grid.</TextBlock>
    </Grid>
  </DockPanel>
</Page>

我只能假设您在问其他问题。但是什么?

【讨论】:

  • 我认为你的权利,但这似乎只适用于根布局。
【解决方案3】:

我找到了解决方法,但很想使用其他解决方案。我使用了滚动查看器并隐藏了滚动条。这似乎是一个沉重的解决方案,但暂时可行。当我把它移到 Windows Phone 7 时,我肯定需要找到更轻的东西。

<UserControl 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" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d"
x:Class="SilverlightApplication2.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White" >
    <ScrollViewer x:Name="scrollViewer" Margin="0,0,158,0" VerticalScrollBarVisibility="Disabled" Height="123" VerticalAlignment="Top">
        <Grid x:Name="grid" VerticalAlignment="Top" MaxWidth="169" MaxHeight="145" Height="141" RenderTransformOrigin="0.5,0.5">
            <Grid.RenderTransform>
                <CompositeTransform/>
            </Grid.RenderTransform>
            <Button Content="Button" Margin="25,42,0,60" HorizontalAlignment="Left" Width="71"/>
            <Button Content="Button" Height="76" Margin="25,0,0,-83" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="81"/>
        </Grid>
    </ScrollViewer>
</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    相关资源
    最近更新 更多