2010-07-22 55 views
0

我有一個UserControl包含一個TextBox。 TextBox.Text屬性是數據綁定和驗證的。當發生Validation.Error時,我想編輯Validation.ErrorTemplate。具體來說,我想用Polyline裝飾它。編輯代碼中的Validation.ErrorTemplate

最終目標是在用戶控件的驗證失敗時在文本下方有一條紅色的波浪線。我可能會把它全部弄錯,B/C這是很難的。

回答

2

您應該只需要在TextBox上設置ErrorTemplate。當TextBox上的某個綁定驗證失敗時,ErrorTemplate將顯示在裝飾器圖層中。你可以通過做這樣的事情畫一條波浪線:

<Validation.ErrorTemplate> 
    <ControlTemplate> 
     <StackPanel> 
      <AdornedElementPlaceholder/> 
      <Rectangle Height="7"> 
       <Rectangle.Fill> 
        <DrawingBrush 
          TileMode="Tile" 
          ViewportUnits="Absolute" 
          Viewport="0 0 4 7" 
          ViewboxUnits="Absolute" 
          Viewbox="0 0 4 7" 
          > 
         <DrawingBrush.Drawing> 
          <GeometryDrawing> 
           <GeometryDrawing.Pen> 
            <Pen Brush="Red" Thickness="1"/> 
           </GeometryDrawing.Pen> 
           <GeometryDrawing.Geometry> 
            <PathGeometry Figures="M0,2 L2,5 4,2, 6,5" /> 
           </GeometryDrawing.Geometry> 
          </GeometryDrawing> 
         </DrawingBrush.Drawing> 
        </DrawingBrush> 
       </Rectangle.Fill> 
      </Rectangle> 
     </StackPanel> 
    </ControlTemplate> 
</Validation.ErrorTemplate> 
+0

這太神奇了。 – pomeroy 2010-07-23 17:34:44

+0

如果您希望該行只擴展到TextBox中的文本,您的策略是什麼?我可以用編程的方式計算出來,但是我怎樣才能把它加入到標記中呢? – pomeroy 2010-07-23 17:47:28

+1

您可以在TextBox上創建具有文本位置的附加屬性,然後將下劃線的位置綁定到該屬性。 – Quartermeister 2010-07-23 18:32:07