验证错误消息显示[WPF]

时间:2016-05-15 19:36:15

标签: c# wpf validation xaml

我正在努力显示验证错误消息。我已设置验证条件,但仍无法显示验证消息。当数据无效时,我想在TextBox上面留一条消息。

我的c#代码如下:

<Window x:Class="WpfApplication9.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication9"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel Name="pnlChildren">
    <ListBox Height="100" Width="120" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name}"/>
                    <TextBlock Text=" ," />
                    <TextBlock Text="{Binding Path=Age}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Text="Name:" />
        <TextBox Text="{Binding Path=Name}" Grid.Column="1" />
        <TextBlock Text="Age:" Grid.Row="1" />
        <TextBox Grid.Row="1" Grid.Column="1" >
            <TextBox.Text>
                <Binding Path ="Age" UpdateSourceTrigger="LostFocus" >
                    <Binding.ValidationRules>

                        <local:AgeValidation />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

    </Grid>
</StackPanel>

和XAML:

namespace ConsoleApplication1
{
    using Row = List<string>;
    using Table = List<Row>;
    using DataBase = Dictionary<string, Table>;

    static class Program
    {
        public static void Main()
        {
            var d = new DataBase();

            Console.Read();
        }
    }
}

0 个答案:

没有答案