Silverlight下TextBlock文字居中对齐的实现

.xaml
< Canvas
    
xmlns ="http://schemas.microsoft.com/client/2007"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Width
="640"  Height ="480"
    Background
="White"
    x:Name
="Page"
    
>
  
< Canvas  Width ="50"  Height ="26"  x:Name ="Do_Button"  Canvas.Left ="35"  Canvas.Top ="11" >
    
< Rectangle  Width ="50"  Height ="26"  Stroke ="#FF212121"  RadiusX ="5"  RadiusY ="5"  StrokeThickness ="2"  x:Name ="RecProgressBg" >
      
< Rectangle.Fill >
        
< LinearGradientBrush  EndPoint ="0.5,1.167"  StartPoint ="0.5,-0.333" >
          
< GradientStop  Color ="#0A0809"  Offset ="0" />
          
< GradientStop  Color ="#0A0809"  Offset ="1" />
          
< GradientStop  Color ="#666666"  Offset ="0.572" />
          
< GradientStop  Color ="#0A0809"  Offset ="0.572" />
        
</ LinearGradientBrush >
      
</ Rectangle.Fill >
    
</ Rectangle >
    
< TextBlock  x:Name ="txtProgress"  Width ="100"  Height ="26"  Canvas.Left ="6"  Canvas.Top ="3"  TextWrapping ="NoWrap" >
    
</ TextBlock >
  
</ Canvas >   
</ Canvas >
.xaml.js
         var  txtProgress = rootElement.findName( " txtProgress " );
        txtProgress.foreground
= " white " ;
        txtProgress.Text 
= " 4/5 " ;
        txtProgress.FontSize
= " 12 " ;
        txtProgress.FontWeight
= " Thin " ;
        
var  RecProgressBg = rootElement.findName( " RecProgressBg " );
        txtProgress.SetValue(
" Canvas.Left " ,(RecProgressBg.Width - txtProgress.ActualWidth) / 2); // 居中对齐实现

你可能感兴趣的:(silverlight)