0

这里我的文本框文本被绑定到内容模板中的子文本框,但是如何将默认样式和验证错误与内容模板中的子文本框一起应用。

    <TextBox x:Name="tbIdNumber"  Width="110" Height="20"  Text="{Binding IdNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" >
        <TextBox.Template>
            <ControlTemplate TargetType="TextBox" >                    
                <StackPanel Orientation="Horizontal" >
                   <Image Height="10" Width="20" Source="Images/bullet_darkblue.PNG" />
                    <TextBox   Width="90"   Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}"    DataContext="{Binding RelativeSource={RelativeSource TemplatedParent},Path=DataContext, Mode=TwoWay}"
                               Text="{Binding  Path=Text ,RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"  >                          
                    </TextBox>
                </StackPanel>
             </ControlTemplate>
        </TextBox.Template>
    </TextBox>
4

1 回答 1

0

用 TextBox 定义 TextBox 控件的控件模板是很不寻常的。我建议你不要走上面的路。

您应该从此处的 TextBox 的默认 ControlTemplate 开始(警告:它非常复杂)。它包含验证、聚焦等所需的所有视觉状态。

根据我的观察,您想在文本框旁边添加一个图像。您应该能够在 Grid 内添加一个图像元素,然后也许可以调整 Border 上的边距。这是一个片段:

<Image Height="10" Width="20" Source="Images/bullet_darkblue.PNG" />
<Border Margin="20,10,0,0" 
        x:Name="MouseOverBorder" BorderThickness="1" BorderBrush="Transparent">
  <ScrollViewer x:Name="ContentElement" Padding="{TemplateBinding Padding}" 
     BorderThickness="0" IsTabStop="False"/>
</Border>
于 2012-05-19T08:17:47.137 回答