2016-08-18 215 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'。 –