【问题标题】:WPF How to display error message next to text box using INotifyDataErrorInfoWPF 如何使用 INotifyDataErrorInfo 在文本框旁边显示错误消息
【发布时间】:2014-09-16 15:31:47
【问题描述】:

我是 WPF 新手,并开始按照下面的教程进行操作。

http://social.technet.microsoft.com/wiki/contents/articles/19490.validating-data-in-wpf-4-5-using-the-inotifyerrordataerror-interface.aspx#Visual_feedback

它使用错误模板来显示如下错误

<Validation.ErrorTemplate>

    <ControlTemplate>

        <StackPanel>

            <!-- Placeholder for the TextBox itself -->

            <AdornedElementPlaceholder x:Name="textBox"/>

            <ItemsControl ItemsSource="{Binding}">

                <ItemsControl.ItemTemplate>

                    <DataTemplate>

                        <TextBlock Text="{Binding ErrorContent}" Foreground="Red"/>

                    </DataTemplate>

                </ItemsControl.ItemTemplate>

            </ItemsControl>

        </StackPanel>

    </ControlTemplate>

</Validation.ErrorTemplate>

我知道了如何在本教程中的文本框下方显示错误消息。但是,我想在文本框旁边而不是在文本框下方显示错误消息。

有办法吗?我试图定义一个新的网格列,并试图将 StackPanel 设置在该新网格的位置,但它不起作用。 (Grid.Column 似乎在那里无效)

【问题讨论】:

  • 寻求帮助的问题必须包含在问题本身中重现问题所需的行为和代码。

标签: c# wpf


【解决方案1】:

你需要调整ErrorTemplate:

<TextBox Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}">
    <Validation.ErrorTemplate>
        <ControlTemplate>

            <!-- Align text box and error list horizontally -->
            <StackPanel Orientation="Horizontal">

                <AdornedElementPlaceholder x:Name="textBox"/>
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ErrorContent}" Foreground="Red"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </ControlTemplate>
    </Validation.ErrorTemplate>
</TextBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多