Silverlight 网格自动调整大小在画布内不起作用
如何使画布内的网格宽度为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
看起来我在这里找到了一个解决方案
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.
还要检查以确保您的表单实际上正在拉伸 Silverlight 应用程序。
Also check to make sure that your form is actually stretching the Silverlight application.