Silverlight 网格自动调整大小在画布内不起作用

发布于 2024-08-24 06:57:39 字数 571 浏览 9 评论 0原文

如何使画布内的网格宽度为 100%?这是一些简单的 XAML,但它没有按预期工作。

<Canvas Background="MediumSlateBlue" Width="Auto" Height="Auto" >
    <Grid x:Name="LayoutRoot" MouseMove="MainPage_MouseMove" Background="Beige" >
        <TextBlock x:Name="lblDisplay" Height="24" HorizontalAlignment="Right" VerticalAlignment="Top" Width="128" Text="asdf" ></TextBlock>
    </Grid>
</Canvas>

我不明白为什么我的网格没有占用尽可能多的空间!我什至尝试添加宽度为 100* 的单个行和列定义,但我的网格仍然只会占用与其包含的标签一样多的空间。目标是拥有一个带有网格子级并占据 100% 宽度和高度的画布。这很重要,因为我需要 silverlight 在浏览器调整大小时调整大小。

How do you make a grid width 100% inside a canvas? Here's some simple XAML but it doesn't work as expected.

<Canvas Background="MediumSlateBlue" Width="Auto" Height="Auto" >
    <Grid x:Name="LayoutRoot" MouseMove="MainPage_MouseMove" Background="Beige" >
        <TextBlock x:Name="lblDisplay" Height="24" HorizontalAlignment="Right" VerticalAlignment="Top" Width="128" Text="asdf" ></TextBlock>
    </Grid>
</Canvas>

I don't understand why my grid doesn't take up as much room as it can get it's hands on! I've even tried adding a single row and column definition with a width of 100*, but still my grid will only take up as much space as the label it contains. The goal is to have a canvas, with a grid child and takes up 100% width and height. This is important because I need the silverlight to resize when the browser resizes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

战皆罪 2024-08-31 06:57:39

Canvas 使用绝对定位来布局其内容。它与 Windows 窗体的工作方式非常相似,所有元素都必须指定顶部、左侧、宽度和高度。

您可以通过将 Canvas 替换为未定义行/列的 Grid 并使用边距放置子元素来实现类似的功能。

Canvas lays out its content using absolute positioning. It's much more similar to the way Windows Forms worked in that all elements must have a top, left, width, and height specified.

You can achieve similar functionality by replacing Canvas with a Grid that has no rows/columns defined and use margins to place child elements.

红尘作伴 2024-08-31 06:57:39

看起来我在这里找到了一个解决方案

http://forums.silverlight.net/forums/t /13415.aspx

我已经调整了代码,以便在调整内容大小时自动调整网格大小。这将允许我使用 Canvas.LeftProperty 绝对定位元素并保持水平/垂直对齐元素的位置。

我考虑过仅使用网格作为我的主要布局,但是如果需要对对象进行动画处理,则无法对边距进行动画处理。此外,在 mousemove 事件期间设置边距 Left 和 Top 并不能准确地将我的元素定位到光标位置。

Looks like I found a solution here

http://forums.silverlight.net/forums/t/13415.aspx

I've adjusted my code to automatically resize my grid whenever the content is resized. This will allow me to position elements absolutly using Canvas.LeftProperty and maintain the position of horizontally/vertically aligned elements.

I considered using just a grid as my primary layout, however should the need arise to animate an object, margin cannot be animated. Additionally, setting the margin Left and Top during the mousemove event did not accurately position my element to the cursor position.

无戏配角 2024-08-31 06:57:39

还要检查以确保您的表单实际上正在拉伸 Silverlight 应用程序。

<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="clientbin/MyApp.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="3.0.40818.0" />
      <param name="autoUpgrade" value="true" />
      <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
      </a>
    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>

Also check to make sure that your form is actually stretching the Silverlight application.

<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="clientbin/MyApp.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="3.0.40818.0" />
      <param name="autoUpgrade" value="true" />
      <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
      </a>
    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文