2

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);
    }
4

1 に答える 1