简体   繁体   English

WPF 数据绑定异常处理

[英]WPF Data Binding exception handling

I have a textbox that is bound to an Integer property.我有一个绑定到 Integer 属性的文本框。 when the user enters something in the textbox that can't be converted to an integer (eg. name) an exception will be thrown and the original property value won't change.当用户在文本框中输入无法转换为 integer(例如名称)的内容时,将引发异常并且原始属性值不会更改。 i want to catch the exception so that i can disable a command that is connected to that property?我想捕获异常以便我可以禁用连接到该属性的命令? how can i do that in general and if possible from the view model where the property is defined?一般来说,如果可能的话,我该如何从定义属性的视图 model 中做到这一点?

I faced the same issue recently and I used behaviors to solve it (but you don't need them if you don't want, it was just for reusing some code I needed among different views). 我最近遇到了同样的问题,并且我使用行为来解决它(但如果你不想要,你就不需要它们,它只是为了重用我在不同视图中需要的代码)。 The main idea is to define some methods in the ViewModel that allow the view to notify errors in the input that the ViewModel cannot detect. 主要思想是在ViewModel中定义一些方法,允许视图通知ViewModel无法检测到的输入中的错误。

So, first define those methods in your ViewModel. 因此,首先在ViewModel中定义这些方法。 For simplicity, I will only keep track of the number of errors, but you can store more info about them (like the actual error): 为简单起见,我只会跟踪错误的数量,但您可以存储更多关于它们的信息(如实际错误):

private int _errorCount = 0;
void AddUIValidationError()
{
   _errorCount++;
}

void RemoveUIValidationError()
{
   _errorCount--;
}

Then, in your View, you register for System.Windows.Controls.Validation.ErrorEvent , which is a routed event that lets you know when a component (previously configured to notify data errors) detects errors (like an exception validation error): 然后,在View中,注册System.Windows.Controls.Validation.ErrorEvent ,这是一个路由事件,可以让您知道组件(先前配置为通知数据错误)何时检测到错误(如异常验证错误):

public partial class MyView : UserControl // or whatever it is
{
    public MyView(MyViewModel viewModel)
    {
        // Possibly ensure that viewModel is not null
        InitializeComponent();
        _myViewModel = viewModel;

        this.AddHandler(System.Windows.Controls.Validation.ErrorEvent, new RoutedEventHandler(OnValidationRaised));
    }

    private MyViewModel _myViewModel;

    private void OnValidationRaised(object sender, RoutedEventArgs e)
    {
        var args = (System.Windows.Controls.ValidationErrorEventArgs)e;

        if (_myViewModel != null)
        {

            // Check if the error was caused by an exception
            if (args.Error.RuleInError is ExceptionValidationRule)
            {
                // Add or remove the error from the ViewModel
                if (args.Action == ValidationErrorEventAction.Added)
                    _myViewModel.AddUIValidationError();
                else if (args.Action == ValidationErrorEventAction.Removed)
                    _myViewModel.RemoveUIValidationError();
            }
        }
    }
}

In the CanExecute method of your Command, you would check if the _errorCount field of your ViewModel is more than 0, and in that case, the command should be disabled. 在Command的CanExecute方法中,您将检查ViewModel的_errorCount字段是否大于0,在这种情况下,应该禁用该命令。

Please note that you should must add ValidatesOnExceptions=True, NotifyOnValidationError=True to your bindings so this can work. 请注意,您必须将ValidatesOnExceptions=True, NotifyOnValidationError=True到绑定中,以便这可以工作。 Ex: 例如:

<TextBox Text="{Binding Path=MyProperty, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />

EDIT: 编辑:

Another approach, apart from what Riley mentioned (which is also good, but requires that you map each integer property from your model to a new string property in your ViewModel) is using ValidationRules. 另一种方法,除了Riley提到的(这也很好,但要求你将模型中的每个整数属性映射到ViewModel中的新字符串属性)都使用ValidationRules。 You can add ValidationRule s that are checked before parsing and calling the property setter. 您可以添加在解析和调用属性setter之前检查的ValidationRule So you could, for example, inherit from ValidationRule and implement the Validate method to ensure that the string can be parsed to an integer. 因此,您可以继承ValidationRule并实现Validate方法,以确保可以将字符串解析为整数。 Example: 例:

public class IntegerValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        int number;
        if(Int32.TryParse((string)value, out number) == false)
            return new ValidationResult(false, "It is not a valid number");
        return new ValidationResult(true, null);
    }
}

And then, in your view define the namespace where IntegerValidationRule is defined: 然后,在您的视图中定义名称空间,其中定义了IntegerValidationRule:

<UserControl 
...
    xmlns:rules="clr-namespace:MyApplication.ValidationRules"
...>

And use the rule in your bindings: 并在绑定中使用规则:

<TextBox>
    <TextBox.Text>
       <Binding Path="MyProperty">
           <Binding.ValidationRules>

              <rules:IntegerValidationRule/>
           </Binding.ValidationRules>
       </Binding>
    </TextBox.Text>
</TextBox>

But anyway, you'll need to create classes for each non-string type you want to validate, and I think the Binding syntax now looks a bit long. 但无论如何,您需要为要验证的每个非字符串类型创建类,我认为Binding语法现在看起来有点长。

Greetings 问候

Consider calling the command from the view model instead of from the view. 考虑从视图模型而不是从视图中调用命令。

private int _myProperty;
public int MyProperty
{
    get
    {
        return _myProperty;
    }
    set
    {
        _myProperty = value;
        // See if the value can be parsed to an int.
        int potentialInt;
        if(int.TryParse(_myProperty, out potentialInt))
        {
            // If it can, execute your command with any needed parameters.
            yourCommand.Execute(_possibleParameter)
        }
    }
}

This will allow you to handle the user typing things that cannot be parsed to an integer, and you will only fire the command when what the user typed is an integer. 这将允许您处理用户键入无法解析为整数的内容,并且只有当用户键入的是整数时才会触发该命令。

(I didn't test this code, but I think it might be helpful.) (我没有测试这段代码,但我认为它可能会有所帮助。)

The "best" solution I've found in the web is explained at http://www.wpfsharp.com/2012/02/03/how-to-disable-a-button-on-textbox-validationerrors-in-wpf/ 我在网上找到的“最佳”解决方案在http://www.wpfsharp.com/2012/02/03/how-to-disable-a-button-on-textbox-validationerrors-in-wpf中有解释。 /

In short, the Validation errors are dealt with in the View, not in the ViewModel. 简而言之,验证错误在View中处理,而不是在ViewModel中处理。 You do not need your own ValidationRule. 您不需要自己的ValidationRule。 Just add a style to your button in XAML: 只需在XAML中为按钮添加样式:

<Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="IsEnabled" Value="false" />
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding ElementName=TextBox1, Path=(Validation.HasError)}" Value="false" />
                            <Condition Binding="{Binding ElementName=TextBox2, Path=(Validation.HasError)}" Value="false" />
                        <Setter Property="IsEnabled" Value="true" />
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>

Hope this helps someone who stumbles upon this question (I am aware that it won't likely help the OP so many years later). 希望这可以帮助那些偶然发现这个问题的人(我知道这么多年后它不会对OP有所帮助)。

We can even directly try avoiding the user from entering any char to the text box我们甚至可以直接尝试避免用户在文本框中输入任何字符

<TextBox Name="txtBox" PreviewTextInput="NumberValidationTextBox"/>

In xaml.cs在 xaml.cs

private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
        e.Handled = Regex.IsMatch(e.Text, "[^0-9]+");
}

We can have check in the Model for this property which checks the value before setting it to avoid any null or empty values我们可以检查这个属性的 Model,它在设置它之前检查值以避免任何 null 或空值

private int _number;
public int Number
{
     get => _number;
     set
     {
        if(!string.IsNullOrEmpty(value.ToString())
            _number = value;
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM