2013-12-15 97 views
0

我正在嘗試製作一個動畫,其中我抓住TextChanged事件並使新添加的字符從字體大小0淡出到由我設置的大小。設置WPF文本框中單個字符的字體大小

我有動畫工作,但是,當我鍵入一個新的字符時,文本框的整個內容會隨之改變,但我只想要按下的鍵。

這是我的代碼:

private void CreateTextboxAnimation(TextBox box) 
    { 
     // Info Needed By Control 
     double FontSize = box.FontSize; 
     // 
     DoubleAnimation Anim = new DoubleAnimation(); 
     Anim.From = 0.0; 
     Anim.To = FontSize; 
     Anim.Duration = new Duration(TimeSpan.FromMilliseconds(100)); 
     Anim.AutoReverse = false; 
     SB = new Storyboard(); 
     SB.Children.Add(Anim); 
     Storyboard.SetTargetName(Anim, box.Name); 
     Storyboard.SetTargetProperty(Anim, new PropertyPath(TextBox.FontSizeProperty)); 

    } 
    private Storyboard SB; 

    private void TestBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     if (TestBox.IsLoaded == true) 
     { 
      SB.Begin(TestBox); 
     } 
    } 
+0

您最好的選擇可能是使用了'RichTextBox'。你將能夠操縱個人角色。 –

回答

2

你不能有控制權的使用標準文本控件在WPF的文字水平。您需要深入探索Glyphs and GlyphRuns的深層世界。使用這個最低級別的功能,您可以完全控制顯示的每個字符。從鏈接頁面:

字形是給定字體中字符的物理表示形式。字符可能有許多字形,系統上的每種字體都可能爲該字符定義不同的字形。

字形運行表示一組連續的字形,它們都具有相同的字體和大小,以及相同的客戶端繪製效果(如果有)。

這裏是MSDN一個很好的網頁介紹Glyph S和GlyphRun S的顯示了一些代碼示例:

Introduction to the GlyphRun Object and Glyphs Element


UPDATE >>>

另一種方法,你本來可以用來增加文字大小的部分將會在內操縱Run元素,但我認爲它不適用於文本輸入。查看StackOverflow上的WPF How to arrange TextBlock with different Fontsize at bottomline這篇文章(僅適用於文本輸出)。

最後,我只是想,你可以做你想做的,如果你創建的char個集合並ListBoxOrientation="Horizontal"設定爲ListBox.ItemsPanel動畫StackPanel自定義顯示它們。您可以簡單地爲每個項目添加動畫。下面是一些關於如何創建自定義動畫面板,簡單地啓動和越來越複雜有用的文章:

How to create a Custom Layout Panel in WPF
Creating a sliding panel in WPF
Animated WPF Panels

相關問題