简体   繁体   English

C#WPF文本块

[英]c# wpf textblock

I need to give space between letter of text-block as 34. 我需要在文本块字母之间留出34个空格。
text-Block has property with Font-Stretch property text-Block具有Font-Stretch属性
but it has its own (Ultraexpanded,condensed, ....such and such) 但它有自己的(超扩展,压缩等)

       <Style x:Key="diningcode" TargetType="TextBlock">


            <Setter Property="FontStretch" Value="UltraExpanded"/>

        </Style>   

I want to change this 'ultraExpanded' property to 34 any solution? 我想将此“ ultraExpanded”属性更改为34任何解决方案?

You may try this : 您可以尝试以下方法:

public class AdvancedStretchTextBlock : TextBlock
{
    /// <summary>
    /// Defines charachter/letter spacing
    /// </summary>
    public int Tracking
    {
        get => (int)GetValue(TrackingProperty);
        set => SetValue(TrackingProperty, value);
    }

    public static readonly DependencyProperty TrackingProperty =
        DependencyProperty.Register("Tracking", typeof(int), typeof(AdvancedStretchTextBlock),
            new UIPropertyMetadata(0,
                TrackingPropertyChanged));

    static void TrackingPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        if (!(o is AdvancedStretchTextBlock tb) || String.IsNullOrEmpty(tb.Text))
            return;

        tb._tracking.X = (int)e.NewValue;
        tb._trackingAlignment.X = -(int)e.NewValue * tb.Text.Length;

        if (tb._lastTrackingTextLength == tb.Text.Length)
            return; // Avoid re-creating effects when you don't have to..

        // Remove unused effects (string has shortened)
        while (tb._trackingEffects.Count > tb.Text.Length)
        {
            tb.TextEffects.Remove(tb._trackingEffects[tb._trackingEffects.Count - 1]);
            tb._trackingEffects.RemoveAt(tb._trackingEffects.Count - 1);
        }

        tb._lastTrackingTextLength = tb.Text.Length;

        // Add missing effects (string has grown)
        for (int i = tb._trackingEffects.Count; i < tb.Text.Length; i++)
        {
            var fx = new TextEffect()
            {
                PositionCount = i,
                Transform = tb._tracking
            };
            tb._trackingEffects.Add(fx);
            tb.TextEffects.Add(fx);
        }

        // Ugly hack to fix overall alignment
        tb.RenderTransform = tb._trackingAlignment;

    }

    private readonly TranslateTransform _tracking = new TranslateTransform();
    private readonly TranslateTransform _trackingAlignment = new TranslateTransform();
    private readonly List<TextEffect> _trackingEffects = new List<TextEffect>();
    int _lastTrackingTextLength;

}

And for the xaml use : 对于xaml使用:

<local:AdvancedStretchTextBlock Text=""... Tracking="-10"/>

ref : https://social.msdn.microsoft.com/Forums/vstudio/en-US/789c3e1b-e3ae-476f-b37f-d93ef6d0cb7b/character-spacing-in-textblocktextelement?forum=wpf 参考: https : //social.msdn.microsoft.com/Forums/vstudio/zh-CN/789c3e1b-e3ae-476f-b37f-d93ef6d0cb7b/character-spacing-in-textblocktextelement? forum =wpf

I believe you can try it out yourself to find out the answer, but obviously you cannot assess -10 to FontStretch property as the property only accept a collection of options as input value. 我相信您可以自己尝试一下以找出答案,但是显然您无法评估-10FontStretch属性,因为该属性仅接受选项的集合作为输入值。 The option you're looking for may be FontStretches.ExtraCondensed . 您正在寻找的选项可能是FontStretches.ExtraCondensed I suggest you go to MSDN to read: https://msdn.microsoft.com/en-us/library/system.windows.fontstretches.extracondensed(v=vs.110).aspx 我建议您去MSDN阅读: https : //msdn.microsoft.com/zh-cn/library/system.windows.fontstretches.extracondensed(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM