2011-07-07 4 views
0

私は、コントロールのTextBoxの領域全体に矩形を設定するSuperTextBというテンプレートを作成しようとしています。この長方形は、検証が失敗したときに検証エラーメッセージを表示するために使用され、残りの時間は隠されます。私がしようとしたことは次のとおりですが、実際には機能しませんでした。Generic.xamlの上に矩形が垂直に積まれたテキストボックスですか?

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:SuperTB"> 
<Style TargetType="{x:Type local:SuperTextB}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:SuperTextB}"> 
       <Border Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}"> 
        <Canvas x:Name="painting" Background="Red" 
          Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height}" 
           Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"> 
         <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus }" x:Name="PART_input" 
           Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Canvas, AncestorLevel=1}, Path=Height}" 
           Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Canvas, AncestorLevel=1}, Path=Width}" 
           Canvas.ZIndex="1"/> 
         <Rectangle x:Name="PART_error" 
           Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Canvas, AncestorLevel=1}, Path=Height}" 
           Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Canvas, AncestorLevel=1}, Path=Width}" 
           Canvas.ZIndex="2"/> 
        </Canvas> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

+0

「実際には機能しませんでしたか」を拡張できますか?なにが問題だったの? – itowlson

答えて

1

あなたはすべてのそれらの幅と高さのバインディングを必要としません。キャンバスをグリッドに置き換え、TextBoxとRectangleをグリッドの子として設定します。 ZIndexは自動的に矩形を上に表示します。矩形の背景をnullに設定するか、表示する準備ができるまで非表示に設定します。例:

<Style TargetType="{x:Type local:SuperTextB}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:SuperTextB}"> 
       <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> 
        <Grid x:Name="painting" Background="Red"> 
         <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus }" x:Name="PART_input"/> 
         <Rectangle x:Name="PART_error" Visibility="Hidden"/> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

素晴らしい、ありがとう! – humanstory

関連する問題