WPF - 在画布
改变文本锚点下面是一些XAML:WPF - 在画布
<Grid Width="200" Height="200">
<Canvas Background="Beige">
<Line X1="0" X2="200" Y1="100" Y2="100" Stroke="Black"/>
<Line X1="100" X2="100" Y1="0" Y2="200" Stroke="Black"/>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Margin="2">
<TextBlock.RenderTransform>
<TranslateTransform X="100" Y="100"/>
</TextBlock.RenderTransform>
hello world1
<LineBreak/>
hello world2
<LineBreak/>
hello world3
</TextBlock>
</Canvas>
</Grid>
我希望文字出现在我Canvas
,而不是右下象限的右上象限。
是否有可能在WPF中做?
目前文本是从全文绘制到底部,我想它是从bottomleft到topright绘制的。我在Canvas
的网络上找不到答案。
在我的生产代码中,文本应该可以是任何长度和高度。
编辑:
我被要求提供一个完全工作的样品,所以这里有云:
XAML:
<Window x:Class="WpfApp1.MainWindow"
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:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<!--The drawing area can be anything, a grid, a panel, a canvas... Can't just use specific alignment tools so I have to use a Transform-->
<Grid x:Name="DrawingArea" Background="Beige" MouseMove="UIElement_OnMouseMove">
<TextBlock x:Name="TextBlock" Margin="2">
hello world1
<LineBreak/>
hello world2
<LineBreak/>
hello world3
</TextBlock>
</Grid>
</Grid>
</Window>
后面的代码:
using System.Windows.Input;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void UIElement_OnMouseMove(object sender, MouseEventArgs e)
{
var mousePos = e.GetPosition(DrawingArea);
TextBlock.RenderTransform = new TranslateTransform(mousePos.X, mousePos.Y);
}
}
}
当前文本出现在默认的Windows鼠标光标下方,我希望它出现在默认的Windows鼠标光标上方。
添加一个网格,你的画布,设置属性 的HorizontalAlignment =“右” VerticalAlignment =“评出的”
<Canvas x:Name="newCanvas">
<Grid>
<Label Content="Hello World!"
HorizontalAlignment="Right"
VerticalAlignment="Top"
/>
</Grid>
你看它,因为TranslateTransform
的右下部分。您将其设置为X=100, Y=100
。如果你设置Y=0
它应该在顶部。
但是,你应该考虑的一些事情,此代码:
为什么要用
TranslateTransform
画布里面?您可以将Canvas.Top
和Canvas.Left
属性添加到TextBlock
,并且应该这样做。仅当您想要动画元素时才使用TranslateTransform
,即使如此,将其设置为(0,0)也更方便,并且仅在动画过程中对其进行更改。为什么你在使用画布?还有更多关于为什么在一个网格内?你应该能够通过使用单个Grid或者甚至是一个DockPanel来实现你想要的。画布仅用于图形,应始终保持在同一位置。对数据有更好的解决方案,特别是如果它在运行时应该改变的话。
如果你想在你的网格的背景交叉,你可以尝试这样的事:
<Grid
Width="200"
Height="200"
Background="Beige">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Line
X1="0"
X2="200"
Y1="100"
Y2="100"
Stroke="Black"
Grid.RowSpan="2"
Grid.ColumnSpan="2"/>
<Line
X1="100"
X2="100"
Y1="0"
Y2="200"
Stroke="Black"
Grid.RowSpan="2"
Grid.ColumnSpan="2"/>
<TextBlock
VerticalAlignment="Center"
HorizontalAlignment="Center" Margin="2"
Grid.Row="0"
Grid.Column="1">
hello world1
<LineBreak/>
hello world2
<LineBreak/>
hello world3
</TextBlock>
</Grid>
我正在使用'Canvas'和'Translation'矩阵,因为这就是我正在使用的(类似于CAD的情况)。使用'Canvas.Top'和'Canvas.Left'对我来说是不可能的,因为元素不在画布内(它们在'ContentPresenter'内部) – ManIkWeet
那么,为什么你不只是减去mousePos.Y将TextBlock的的ActualHeight? – Clemens
目前我知道使用代码隐藏的文本高度,但在我的实际应用程序(基于MVVM)我无法获得文本的高度。所以它需要以某种方式从ValueConverter或其他东西的文本中的“Transform”中减去。我不知道如何做到这一点? – ManIkWeet
所以你的问题中的代码再次没有显示你实际在做什么。你期望在这里得到什么支持? – Clemens