2016-04-02 5 views

答えて

0

UserControlを使用すると、ここでは「SeparateLetterControl」という名前のコントロールを作成しました。

XAMLコード:背後

<Grid> 
    <StackPanel x:Name="stackPanel" Orientation="{x:Bind LetterOritation}" /> 
</Grid> 

コード:

<local:SeparateLetterControl OriginalText="Hello, World!" LetterOritation="Horizontal" LetterSpacing="-10" LetterSize="50" 
          LetterColors="Cyan,Magenta,Yellow,Black" VerticalAlignment="Center" HorizontalAlignment="Center" /> 

今あなたが表示されます。

public sealed partial class SeparateLetterControl : UserControl 
{ 
    private char[] Letterlist; 

    private Color[] ColorList; 

    public SeparateLetterControl() 
    { 
     this.InitializeComponent(); 
     this.Loaded += SeparateLetterControl_Loaded; 
    } 

    private void SeparateLetterControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     Letterlist = WordToArray(OriginalText); 
     var LetterstringColorArray = LetterColors.Split(','); 
     int colorcount = 0; 
     ColorList = new Color[LetterstringColorArray.Count()]; 
     foreach (var color in LetterstringColorArray) 
     { 
      ColorList[colorcount] = ConvertToColor(color); 
      colorcount++; 
     } 
     int count = 0; 
     if (LetterOritation == Orientation.Horizontal) 
     { 
      foreach (var i in Letterlist) 
      { 
       TextBlock tb = new TextBlock(); 
       tb.Text = i.ToString(); 
       tb.FontSize = 30; 
       tb.Margin = new Thickness(0, 0, LetterSpacing, 0); 
       tb.Foreground = new SolidColorBrush(ColorList[count % ColorList.Count()]); 
       stackPanel.Children.Add(tb); 
       count++; 
      } 
     } 
     else 
     { 
      foreach (var i in Letterlist) 
      { 
       TextBlock tb = new TextBlock(); 
       tb.Text = i.ToString(); 
       tb.FontSize = 30; 
       tb.Margin = new Thickness(0, 0, 0, LetterSpacing); 
       tb.Foreground = new SolidColorBrush(ColorList[count % ColorList.Count()]); 
       stackPanel.Children.Add(tb); 
       count++; 
      } 
     } 
    } 

    //change original word to string[] 
    private char[] WordToArray(string originalword) 
    { 
     return originalword.ToCharArray(); 
    } 

    //convert string to color 
    public static Color ConvertToColor(string colorName) 
    { 
     if (string.IsNullOrEmpty(colorName)) throw new ArgumentNullException("colorName"); 
     MethodBase getColorMethod = FindGetColorMethod(colorName); 
     if (getColorMethod == null) 
     { 
      // Using FormatException like the WPF System.Windows.Media.ColorConverter 
      throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Color name {0} not found in {1}", 
       colorName, typeof(Colors).FullName)); 
     } 
     return (Color)getColorMethod.Invoke(null, null); 
    } 

    private static MethodBase FindGetColorMethod(string colorName) 
    { 
     foreach (PropertyInfo propertyInfo in typeof(Colors).GetTypeInfo().DeclaredProperties) 
     { 
      if (propertyInfo.Name.Equals(colorName, StringComparison.OrdinalIgnoreCase)) 
      { 
       MethodBase getMethod = propertyInfo.GetMethod; 
       if (getMethod.IsPublic && getMethod.IsStatic) 
        return getMethod; 
       break; 
      } 
     } 
     return null; 
    } 

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("OriginalText", typeof(string), typeof(SeparateLetterControl), new PropertyMetadata(string.Empty)); 

    public string OriginalText 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public static readonly DependencyProperty LetterSpacingProperty = DependencyProperty.Register("LetterSpacing", typeof(int), typeof(SeparateLetterControl), new PropertyMetadata(0)); 

    public int LetterSpacing 
    { 
     get { return (int)GetValue(LetterSpacingProperty); } 
     set { SetValue(LetterSpacingProperty, value); } 
    } 

    public static readonly DependencyProperty LetterOritationProperty = DependencyProperty.Register("LetterOritation", typeof(Orientation), typeof(SeparateLetterControl), new PropertyMetadata(Orientation.Horizontal)); 

    public Orientation LetterOritation 
    { 
     get { return (Orientation)GetValue(LetterOritationProperty); } 
     set { SetValue(LetterOritationProperty, value); } 
    } 

    public static readonly DependencyProperty LetterSizeProperty = DependencyProperty.Register("LetterSize", typeof(double), typeof(SeparateLetterControl), new PropertyMetadata(15)); 

    public double LetterSize 
    { 
     get { return (double)GetValue(LetterSizeProperty); } 
     set { SetValue(LetterSizeProperty, value); } 
    } 

    public static readonly DependencyProperty LetterColorProperty = DependencyProperty.Register("LetterColors", typeof(string), typeof(SeparateLetterControl), new PropertyMetadata("Black")); 

    public string LetterColors 
    { 
     get { return (string)GetValue(LetterColorProperty); } 
     set { SetValue(LetterColorProperty, value); } 
    } 
} 

今、あなたはあなたの例のためにそれを使用することができますが、これを好きMainPage.xamlをレンダリングイメージ:

enter image description here

「OriginalText」、「LetterSpacing」、「LetterOritation」、「LetterSize」、「LetterColors」の4つのプロパティが公開されています。あなたが財産OriginalTextにテキストを設定することができます

LetterSpacingプロパティは、それぞれの文字の間のスペースであるLetterOritationは「水平」または「垂直」に設定することができ、LetterSizeは、各文字のFontSizeを表し、LetterColors

[重要!]今LetterColorsプロパティの型が文字列であり、このプロパティの形式は、あなたが望む任意の色や色の任意の数を定義することができる「色、色、色」のようにする必要がありますすることで ます私はこれらの例外を処理しませんでした。空白でさえもエラーが発生する可能性があります。

+0

私はFontFamilyプロパティをここに公開していませんでしたが、とにかく、私のコードアッパーを参照してこの作業を完了することができます。 –

関連する問題