我有一个LayoutTransformControl,其中有一个包含图像和画布的Grid。在应用程序中的某个位置,单击按钮时,我正在Canvas中动态添加一些Thumb控件以实现拖放。问题是如果我为LayoutTransformControl设置了角度,则单击同一按钮时,我会假设它先绘制所有Thumb控件,然后在我首先创建Canvas和Thumbs时翻转Layout控件,但似乎正在更新整个布局和Thumb控件都超出了Canvas的位置。有没有一种方法可以先渲染所有我的Thumb,然后更改角度,以便将整个控件旋转到该角度。
如果角度为0,即如果我不应用变换,则Thumb控件在另一个下方出现,如下所示。
当我说角度为270时,这是我的问题,Thumb控件从画布移开。
xaml.cs
private void BtnCapture_Click(object sender, RoutedEventArgs e)
{
BarCodeImage.Height = cnvBarCodeImage.Height = MainLayoutControl.Height=480;
BarCodeImage.Width = cnvBarCodeImage.Width = MainLayoutControl.Width;
//This code will create the canvas.
for (int i = 0; i < 2; i++)
{
var item = Selected.WindowLocations[i];
var dimensionsItem = new Dimensions();
dimensions.Add(new Dimensions()
{
Height = 262,
Width = 142,
Left = 395,
Top = 44,
Text = string.Empty,
});
dimensions.Add(new Dimensions()
{
Height = 106,
Width = 147,
Left = 395,
Top = 342,
Text = string.Empty,
}
CreateThumbs(2, dimensions); //This will create the Thumbs and add to the Canvas
RotateImage(270);
}
private void RotateImage(int Angle)
{
MainLayoutControl.Transform = new RotateTransform()
{
Angle = Angle
};
}
private void CreateThumbs(int numberOfWindows, List<Dimensions> dimensions)
{
ClearOrRemoveAllChildren();
Thumb th = null;
for (int i = 0; i < numberOfWindows; i++)
{
th = new Thumb();
th.Name = i.ToString();
var item = dimensions[i];
th.Width = item.Width;
th.Height = item.Height;
th.Foreground = new SolidColorBrush(Windows.UI.Colors.Transparent);
th.BorderBrush = item.BorderColor;
th.BorderThickness = new Thickness(3);
th.Template = GetThumbTemplate(item.Text);
th.DragDelta += (sender, e) => Th_DragDelta(sender, e, dimensions);
th.DragCompleted += (sender, e) => Th_DragCompleted(sender, e, item.IsImageRotated);
Canvas.SetLeft(th, item.Left);
Canvas.SetTop(th, item.Top);
cnvBarCodeImage.Children.Add(th);
}
}
这是我的xaml
<uwpControls:LayoutTransformControl x:Name="MainLayoutControl" Grid.Row="4" HorizontalAlignment="Center" VerticalAlignment="Center" Width="640">
<Grid x:Name="gridBarImagePanel">
<Image x:Name="BarCodeImage"
RenderTransformOrigin="0.5,0.5"></Image>
<Canvas x:Name="cnvBarCodeImage" AllowDrop="True">
</Canvas>
</Grid>
</uwpControls:LayoutTransformControl>
对于画布,子内容如果大于面板,则不会在视觉上被剪切,并且不受面板边界的限制,因此将具有这种效果。将画布的高度设置为480,宽度设置为640,将LayoutTransformControl的内容旋转270度时,LayoutTransformControl的高度仍为480,宽度为640,但是画布的高度变为640,画布超出了范围LayoutTransformControl的位置,因此拇指将显示为这样。
您可以将画布和图像的高度和宽度都设置为480,在这种情况下,无论旋转画布还是旋转画布,它始终在LayoutTransformControl的范围内。然后重新布置两个拇指。
BarCodeImage.Height = cnvBarCodeImage.Height = MainLayoutControl.Height = 480;
BarCodeImage.Width = cnvBarCodeImage.Width = MainLayoutControl.Width = 480;
或者,如果仍要保持640的宽度和480的高度,则可以在旋转LayoutTransformControl之后调整其大小。
private void RotateImage(int Angle)
{
MainLayoutControl.Transform = new RotateTransform()
{
Angle = Angle
};
MainLayoutControl.Height = 640;
MainLayoutControl.Width = 480;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句