如何使错误与文本框和标签大小相同?

时间:2014-01-19 18:22:23

标签: c# wpf

所以我设置了一个文本框和标签:

        <TextBox Height="23" HorizontalAlignment="Right" Margin="0,74,119,0" Name="txb_idleTime" VerticalAlignment="Top" Width="162">
            <Validation.ErrorTemplate>
                <ControlTemplate>
                    <StackPanel>
                        <!-- Placeholder for the TextBox itself -->
                        <AdornedElementPlaceholder x:Name="textBox"/>
                        <TextBlock Text="{Binding [0].ErrorContent}" Foreground="Red"/>
                    </StackPanel>
                </ControlTemplate>
            </Validation.ErrorTemplate>
            <TextBox.Text>
                <Binding Path="MinIdleTime" UpdateSourceTrigger="PropertyChanged" >
                    <Binding.ValidationRules>
                        <Local:NumberValidation />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Label Content="Minimum Idle Time (min):" Height="28" HorizontalAlignment="Left" Margin="65,72,0,0" Name="label1" VerticalAlignment="Top" Width="160" HorizontalContentAlignment="Right" Target="{Binding}" />

目前,错误消息直接显示在文本框下面,如下所示:

enter image description here

我希望它看起来像这样:

enter image description here

我该怎么做?

2 个答案:

答案 0 :(得分:1)

指定负左边距到错误textBlock 等于label的宽度。这应该有效:

<TextBlock Text="{Binding [0].ErrorContent}" Margin="-160,0,0,0"
           Foreground="Red"/>

答案 1 :(得分:0)

我可能会采用这种方法:MSDN Validation。我还没有使用它,但听起来好像它符合你的需要。