对 XAML 使用带有 ItemsControl 的 MVVM 模式,我试图将 FormattedText 字符串的左上墨点准确定位在指定矩形的左上角。(矩形由 InkAnalyzer.Location.GetBounds() 给出)。
由于我无法弄清楚(或在 Google 上找到)的原因,FormattedText 字符串始终位于矩形左上角的右下角,距离似乎与用于打印的字体大小成正比。
如何实现 FormattedText 的定位以击中矩形的左上角?
任何帮助是极大的赞赏。
XAML
<ItemsControl
ItemsSource="{Binding Transcription}"
>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas
Background="Transparent"
Width="{x:Static h:Constants.widthCanvas}"
Height="{x:Static h:Constants.heightCanvas}"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
C#
private ObservableCollection<StringViewModel> transcription = new ObservableCollection<StringViewModel>();
public ObservableCollection<StringViewModel> Transcription
{
get
{
return transcription;
}
}
public class StringViewModel : FrameworkElement, INotifyPropertyChanged
{
public StringViewModel(
Point topleft,
string text,
double fontsize,
SolidColorBrush brush,
double linewidth,
double columnheight,
Func<FormattedText,FormattedText> f)
{
this.text = text;
this.FontSize = fontsize * (72.0/96.0);
this.color = brush;
this.origin = topleft;
this.origin_X = topleft.X;
this.origin_Y = topleft.Y;
this.maxtextwidth = linewidth * (72.0/96.0);
this.maxtextheight = columnheight ;
this.f = f;
}
protected override void OnRender(DrawingContext dc)
{
FormattedText ft = new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily("Segoe Script"), FontStyles.Italic, FontWeights.Normal, FontStretches.Normal),
FontSize,
color);
ft.TextAlignment = TextAlignment.Left;
ft.MaxTextWidth = maxtextwidth;
ft.MaxTextHeight = maxtextheight;
dc.DrawText(ft, origin);
}