2016-08-18 214 views
1

我從互聯網上覆制基於單元的文本框類和類定義看起來如下:如何設置背景顏色?

using System; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 

namespace SapQmWp.Classes 
{ 
    public class UnitTextBox : TextBox 
    { 
    public static DependencyProperty UnitTextProperty = 
     DependencyProperty.Register(
     "UnitText", 
     typeof(string), 
     typeof(UnitTextBox), 
     new FrameworkPropertyMetadata(
      default(string), 
      FrameworkPropertyMetadataOptions.AffectsMeasure | 
      FrameworkPropertyMetadataOptions.AffectsArrange | 
      FrameworkPropertyMetadataOptions.AffectsRender)); 

    public static DependencyProperty UnitPaddingProperty = 
     DependencyProperty.Register(
     "UnitPadding", 
     typeof(Thickness), 
     typeof(UnitTextBox), 
     new FrameworkPropertyMetadata(
      new Thickness(5d, 0d, 0d, 0d), 
      FrameworkPropertyMetadataOptions.AffectsMeasure | 
      FrameworkPropertyMetadataOptions.AffectsArrange | 
      FrameworkPropertyMetadataOptions.AffectsRender)); 

    public static DependencyProperty TextBoxWidthProperty = 
     DependencyProperty.Register(
     "TextBoxWidth", 
     typeof(double), 
     typeof(UnitTextBox), 
     new FrameworkPropertyMetadata(
      double.NaN, 
      FrameworkPropertyMetadataOptions.AffectsMeasure)); 

    private FormattedText _unitText; 
    private Rect _unitTextBounds; 

    public string UnitText 
    { 
     get { return (string) GetValue(UnitTextProperty); } 
     set { SetValue(UnitTextProperty, value); } 
    } 

    public Thickness UnitPadding 
    { 
     get { return (Thickness) GetValue(UnitPaddingProperty); } 
     set { SetValue(UnitPaddingProperty, value); } 
    } 

    public double TextBoxWidth 
    { 
     get { return (double) GetValue(TextBoxWidthProperty); } 
     set { SetValue(TextBoxWidthProperty, value); } 
    } 

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    { 
     base.OnPropertyChanged(e); 

     if (e.Property == ForegroundProperty) 
     EnsureUnitText(true); 
    } 

    protected override Size MeasureOverride(Size constraint) 
    { 
     var textBoxWidth = TextBoxWidth; 
     var unit = EnsureUnitText(true); 
     var padding = UnitPadding; 

     if (unit != null) 
     { 
     var unitWidth = unit.Width + padding.Left + padding.Right; 
     var unitHeight = unit.Height + padding.Top + padding.Bottom; 

     constraint = new Size(
      constraint.Width - unitWidth, 
      Math.Max(constraint.Height, unitHeight)); 
     } 

     var hasFixedTextBoxWidth = !double.IsNaN(textBoxWidth) && 
           !double.IsInfinity(textBoxWidth); 

     if (hasFixedTextBoxWidth) 
     constraint = new Size(textBoxWidth, constraint.Height); 

     var baseSize = base.MeasureOverride(constraint); 
     var baseWidth = hasFixedTextBoxWidth ? textBoxWidth : baseSize.Width; 

     if (unit != null) 
     { 
     var unitWidth = unit.Width + padding.Left + padding.Right; 
     var unitHeight = unit.Height + padding.Top + padding.Bottom; 

     return new Size(
      baseWidth + unitWidth, 
      Math.Max(baseSize.Height, unitHeight)); 
     } 

     return new Size(baseWidth, baseSize.Height); 
    } 

    protected override Size ArrangeOverride(Size arrangeBounds) 
    { 
     var textSize = arrangeBounds; 
     var unit = EnsureUnitText(false); 
     var padding = UnitPadding; 

     if (unit != null) 
     { 
     var unitWidth = unit.Width + padding.Left + padding.Right; 
     var unitHeight = unit.Height + padding.Top + padding.Bottom; 

     textSize.Width -= unitWidth; 

     _unitTextBounds = new Rect(
      textSize.Width + padding.Left, 
      (arrangeBounds.Height - unitHeight)/2 + padding.Top, 
      textSize.Width, 
      textSize.Height); 
     } 

     var baseSize = base.ArrangeOverride(textSize); 

     if (unit != null) 
     { 
     var unitWidth = unit.Width + padding.Left + padding.Right; 
     var unitHeight = unit.Height + padding.Top + padding.Bottom; 

     return new Size(
      baseSize.Width + unitWidth, 
      Math.Max(baseSize.Height, unitHeight)); 
     } 

     return baseSize; 
    } 

    protected override void OnRender(DrawingContext drawingContext) 
    { 
     base.OnRender(drawingContext); 
     var unitText = EnsureUnitText(false); 
     if (unitText != null) 
     drawingContext.DrawText(unitText, _unitTextBounds.Location); 
    } 

    private FormattedText EnsureUnitText(bool invalidate = false) 
    { 
     if (invalidate) 
     _unitText = null; 

     if (_unitText != null) 
     return _unitText; 

     var unit = UnitText; 

     if (!string.IsNullOrEmpty(unit)) 
     { 
     _unitText = new FormattedText(
      unit, 
      CultureInfo.InvariantCulture, 
      FlowDirection, 
      new Typeface(
      FontFamily, 
      FontStyle, 
      FontWeight, 
      FontStretch), 
      FontSize, 
      Foreground); 
     } 

     return _unitText; 
    } 
    } 
} 

我插入到窗口XAML如下:

... 
<ui:UnitTextBox Grid.Row="1" Style="{StaticResource WeightTbStyle}" UnitText="KG" Text="{Binding TargetWeight, UpdateSourceTrigger=PropertyChanged}"/> 
... 

但底色爲空,而不是顏色。
enter image description here

我的問題,如何設置UnitText的背景顏色?

更新

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:SapQmWp.Themes"> 

    <Style x:Key="WeightTbStyle" TargetType="TextBox"> 
    <Setter Property="Background" Value="#F8F8F8" /> 
    <Setter Property="BorderBrush" Value="{x:Null}"/> 
    <Setter Property="Margin" Value="10,40,10,40" /> 
    <Setter Property="Padding" Value="0,0,0,5" /> 
    <Setter Property="HorizontalContentAlignment" Value="Center" /> 
    <Setter Property="VerticalContentAlignment" Value="Center" /> 
    <Setter Property="Foreground" Value="#404242"/> 
    <Setter Property="FontSize" Value="24pt"/> 
    <Setter Property="FontWeight" Value="Bold"/> 
    <Setter Property="BorderThickness" Value="0" /> 
    </Style> 


</ResourceDictionary> 

更新2
當我設置上的控件的背景像

<ui:UnitTextBox Grid.Row="1" Background="Tomato" Style="{StaticResource WeightTbStyle}" UnitText="KG" Text="{Binding TargetWeight, UpdateSourceTrigger=PropertyChanged}"/> 

的結果,我已經有了:
enter image description here

+1

由於UnitTextBox從TextBox繼承,它沒有背景屬性,你可以設置? –

+0

所以,當我將background屬性設置爲'Background =「Blue」'時,它對'UnitText'背景色沒有任何影響。 –

+0

''試試這個或設置您所指的相應樣式中的背景屬性值。 – ViVi

回答

0

在你

protected override void OnRender(DrawingContext drawingContext) 
    { 
     base.OnRender(drawingContext); 
     var unitText = EnsureUnitText(false); 
     if (unitText != null) 
     drawingContext.DrawText(unitText, _unitTextBounds.Location); 
    } 

嘗試這樣的事情來設置顏色和格式

 var formattedText = new FormattedText(unitText, 
      CultureInfo.CurrentCulture, 
      FlowDirection.LeftToRight, 
      new Typeface(new FontFamily("ANY_FONT_FAMILY"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal), 
      24, Brushes.Red); 

     drawingContext.DrawText(formattedText, _unitTextBounds.Location); 
    } 

與背景顏色只是畫一個矩形,你使您的文本之前。

drawingContext.DrawRectangle(Brushes.Red, Nothing, YOUR_TEXT_RECT); 
+0

yikes的更新答案!你問過文字顏色還是背景顏色?忽略這個如果我想用 – Muds

+0

來獲得背景顏色,在繪製文本之前畫一個矩形 – Muds

+0

'UnitText'應該採用背景顏色,我在左上角顯示'KG'。 –