C# 使用wpf验证c中的文本框

C# 使用wpf验证c中的文本框,c#,wpf,validation,styles,C#,Wpf,Validation,Styles,我有一个带有文本框、组合框和复选框的窗口。其中一个文本框需要是一个数字,所以我想验证它。我在网上搜索了一下,发现了一个很好的答案。我试着使用它,但它似乎不起作用,或者我做错了什么,我只是不知道我做错了什么。因此,我希望在座的任何人都能告诉我我做错了什么,或者找到一个其他解决方案来开始。 以下是窗口的xaml: <Window x:Class="WpfApplication1.mainpanels.EditWorkAssignments" xmlns="http://schemas.

我有一个带有文本框、组合框和复选框的窗口。其中一个文本框需要是一个数字,所以我想验证它。我在网上搜索了一下,发现了一个很好的答案。我试着使用它,但它似乎不起作用,或者我做错了什么,我只是不知道我做错了什么。因此,我希望在座的任何人都能告诉我我做错了什么,或者找到一个其他解决方案来开始。 以下是窗口的xaml:

<Window x:Class="WpfApplication1.mainpanels.EditWorkAssignments"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:validators="clr-namespace:WpfApplication1.validationRules"
    Title="EditWorkAssignments" Height="225" Width="300">
<Window.Resources>
    <Style TargetType="{x:Type TextBox}">

        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
            Value="{Binding RelativeSource={RelativeSource Self}, 
                   Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="200" />
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Content="Datum:"/>
    <Label Grid.Row="1" Grid.Column="0" Content="Projekt:"/>
    <Label Grid.Row="2" Grid.Column="0" Content="Ist Passiv:"/>
    <Label Grid.Row="3" Grid.Column="0" Content="Dauer:"/>
    <Label Grid.Row="4" Grid.Column="0" Content="Mitarbeiter:"/>
    <DatePicker Name="datePicker" Grid.Column="1" Grid.Row="0" Margin="3" />
    <ComboBox Name="comboBoxProject" Grid.Column="1" Grid.Row="1" Margin="3" />
    <CheckBox Name="checkBoxIsPassiv" Grid.Column="1" Grid.Row="2" Margin="3" />
    <TextBox Name="textBoxDauer" Grid.Column="1" Grid.Row="3" Margin="3" >
        <Binding Path="workAssignment.duration" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <validators:IsNumberValidationRule ErrorMessage="Dauer has to be a number." />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>
        <ComboBox Name="comboBoxEmployee" Grid.Column="1" Grid.Row="4" Margin="3">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} {1}">
                            <Binding Path="firstname"/>
                            <Binding Path="surname"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <Button Grid.Column="1" Grid.Row="5" HorizontalAlignment="Left" 
        MinWidth="80" Margin="3" Content="Save"  Click="saveHandler"/>
    <Button Grid.Column="1" Grid.Row="5" HorizontalAlignment="Right" 
        MinWidth="80" Margin="3" Content="Cancel" Click="cancelHandler" />
</Grid>
您需要为绑定到textbox、textBoxDauer的属性设置一个默认值,或者在代码中创建属性,例如持续时间。在该属性中,然后执行如下验证

public string Duration 
{
    get { return _duration ; }
    set
    {
        _name = value;
        if (String.IsNullOrEmpty(value) || Int32.TryParse(value))
        {
            throw new ArgumentException("Dauer has to be a number..");
        }
    }
}
这个set方法是UpdateSourceTrigger调用的,如果抛出异常,它将向上传播到树上,并且一切都应该按预期工作


希望能有帮助。

解决我的问题真的很容易。我刚刚错过了将workassignment设置为我的dataContext:

        this.DataContext = workAssignement;

Sarajog

错误请您指出没有错误,只是当我在文本框中输入addfztg时没有显示错误消息,该文本框只应包含数字,并且没有红色边框。您的代码不起作用?看起来不错。任何错误或异常?它可以工作,但它既不显示工具提示,也不显示文本框的红色边框。您可以在已验证的文本框中添加任何内容,但它不会显示它只允许包含数字。请检查WorkAssignment类中定义的duration属性have getter和setter。i、 与公共字符串duragion{get;set;}类似,它是一个编辑从数据库生成的实体对象的窗口。我不想为验证手动创建对象。如果我可以使用默认的UpdateSourceTrigger,我也可以这么做,但那也不行。您阅读了本教程中的自定义验证规则了吗?您可以看到,您不希望在每次输入单个字符时更新数据库。UpdateSourceTrigger=PropertyChanged就是这样做的。我建议您使用UpdateSourceTrigger=Explicit或LostFocus。无论哪种方式,在更新数据库之前,都应该检查我建议的属性中的值。如果它通过了验证,那么继续并将其保存到数据库中。有人知道我的问题是什么吗?我只是想告诉用户当他改变文本框的值时有什么问题,就像教程中显示的那样。我向您展示的代码中甚至没有实现数据的保存。当然,在保存数据之前,我会再次验证数据,但我想在用户单击“保存”按钮之前告诉用户出了什么问题。对我来说,使用LostFocus或PropertyChanged并不重要,因为它们在我的代码中都不起作用。
public string Duration 
{
    get { return _duration ; }
    set
    {
        _name = value;
        if (String.IsNullOrEmpty(value) || Int32.TryParse(value))
        {
            throw new ArgumentException("Dauer has to be a number..");
        }
    }
}
        this.DataContext = workAssignement;