在 Grid 或 Stackpanel 中独立调整子级的大小
我有一个图像想要在触摸应用程序的控件中显示。除了图像之外,其下方还显示一个文本框,作为选项卡,停靠在图像的左下角。选项卡的宽度小于图像的宽度。用户可以调整图像大小、移动和旋转图像,但我希望文本框选项卡保持相同的大小以及与图像相同的相对位置。
我尝试过使用 StackPanel 和 Grid,但两次文本框/选项卡以及图像都被放大。
Grid 或 StackPanel 是可行的方法吗?如果是的话,当第一个子项的大小发生变化时,如何强制执行文本框/选项卡(即第二个子项)的大小?
谢谢!
回应 Lars:
<Grid Name="mygrid" Background="Red" Width="320" Height="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="255"/>
<RowDefinition MaxHeight="40" />
</Grid.RowDefinitions>
<!--Ist child-->
<Canvas Name="maincanvas" Background="DarkKhaki" Width="300" Height="180" Grid.Row="0">
<!--<Image goes in here>-->
</Canvas>
<!--2nd child-->
<DockPanel Name="dockpanel" Grid.Row="1" Background="DarkKhaki" MaxWidth="200" HorizontalAlignment="Left">
<TextBlock Name="textblock" TextWrapping="Wrap" >
some text here
</TextBlock>
</DockPanel>
</Grid>
我想要做的是允许用户拖动图像(第一个子元素)并调整其大小,同时保持 TextBlock(第二个子元素)的大小和相对位置。因此,效果是固定在图像左下角的选项卡,因为图像可以动态调整大小。
我尝试添加图像以使其更清晰,但作为新用户我不能,抱歉!
I have an image I want to display in a control in a touch application. As well as the image there is a textbox to display underneath it, as a tab, docked at the bottom left of the image. The tab has a lower width than the image. The user can resize, move and rotate the image, but I'd like the textbox tab to stay the same size and in the same relative position to the image.
I've tried using both a StackPanel and a Grid, but both times the textbox/tab is scaled up as well as the image.
Are either a Grid or StackPanel the way to go, and if so how can I enforce the size of the textbox/tab (that is, the second child) as the size of the first child changes?
Thanks!
In respones to Lars:
<Grid Name="mygrid" Background="Red" Width="320" Height="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="255"/>
<RowDefinition MaxHeight="40" />
</Grid.RowDefinitions>
<!--Ist child-->
<Canvas Name="maincanvas" Background="DarkKhaki" Width="300" Height="180" Grid.Row="0">
<!--<Image goes in here>-->
</Canvas>
<!--2nd child-->
<DockPanel Name="dockpanel" Grid.Row="1" Background="DarkKhaki" MaxWidth="200" HorizontalAlignment="Left">
<TextBlock Name="textblock" TextWrapping="Wrap" >
some text here
</TextBlock>
</DockPanel>
</Grid>
What I want to do is allow the user to drag and resize the Image(1st child), while maintaining the size and relative position of the TextBlock (2nd child). So the effect is of a tab anchored to the bottom left of the image that is fixed as the image can dynamically resize.
I tried to add images to make this clearer but as a new user I can't, sorry!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
讽刺的是,您在 XAML 中使用了 DockPanel 来替换帖子中的图像。 :) 我认为这正是您想要的 - 一个 DockPanel,其顶部部分可以缩放其内容,而下部则不能。
关键是最后将图像容器添加到 DockPanel,以便它自动获得剩余的屏幕空间。并将其 Dock 属性设置为“Top”。
例如(我还没有测试过这个,顺便说一句。我只是认为这会比我之前发布的更好一点)
所以这里的想法是画布中的图像位置数据绑定到代码隐藏中的属性。同样,TabControl 的
Margin
属性也通过数据绑定到该属性。但为了使用它,您需要在代码后面编写一个IValueConverter
,该转换器将获取ImageX
的值并返回一个新的Thickness使用
对象。ImageX
作为左边距的例如
It's ironic that you used a DockPanel in your XAML replacement for an image in your post. :) I think that's really what you want here -- a DockPanel with the top portion that scales its contents, and a lower portion that doesn't.
The key is to add the image container to the DockPanel last, so that it automatically is given the rest of the screen real estate. And set its Dock property to "Top".
e.g. (I haven't tested this, BTW. I am just thinking that this will work a little better than what I posted earlier)
So the idea here is that your image position in the canvas is databound to a property in code-behind. Likewise, the TabControl's
Margin
property is databound to this same property. But in order to use it, you'll need to write anIValueConverter
in code behind, and this converter will take the value ofImageX
and return a newThickness
object that usesImageX
as its left margin.e.g.
再次重申:LayoutTransform 是一个非常非常简单的解决方案。我将主图像和标签放入堆栈面板中。然后,我将缩放变换作为 LayoutTransform 而不是 RenderTransform 应用于图像,并将平移和旋转变换应用于堆栈面板。问题解决了:-)
Will here again: LayoutTransform was a much, much simpler solution to this. I put the main image and the label into a stackpanel. Then I applied the scale transform to the image as a LayoutTransform rather than a RenderTransform, and applied the Translate and Rotate transforms to the stackpanel. Problem solved :-)