[英]c# wpf textblock
我需要在文本塊字母之間留出34個空格。
text-Block具有Font-Stretch屬性
但它有自己的(超擴展,壓縮等)
<Style x:Key="diningcode" TargetType="TextBlock">
<Setter Property="FontStretch" Value="UltraExpanded"/>
</Style>
我想將此“ ultraExpanded”屬性更改為34任何解決方案?
您可以嘗試以下方法:
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;
}
對於xaml使用:
<local:AdvancedStretchTextBlock Text=""... Tracking="-10"/>
參考: https : //social.msdn.microsoft.com/Forums/vstudio/zh-CN/789c3e1b-e3ae-476f-b37f-d93ef6d0cb7b/character-spacing-in-textblocktextelement? forum =wpf
我相信您可以自己嘗試一下以找出答案,但是顯然您無法評估-10
到FontStretch
屬性,因為該屬性僅接受選項的集合作為輸入值。 您正在尋找的選項可能是FontStretches.ExtraCondensed
。 我建議您去MSDN閱讀: https : //msdn.microsoft.com/zh-cn/library/system.windows.fontstretches.extracondensed(v=vs.110).aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.