简体   繁体   English

如何使用mvvm在wpf中为依赖对象编写自定义设置程序

[英]how to write a custom setter for a dependency object in wpf using mvvm

how to write a custom setter for a dependency object in wpf using mvvm ? 如何使用mvvm在wpf中为依赖对象编写自定义设置程序?

In my ViewModel I have a dependency object called Seasonalprop which I use to bind to a TextBox in XAML. 在我的ViewModel中,我有一个名为Seasonalprop的依赖对象,该对象用于绑定到XAML中的TextBox。 I would like to write a custom setter, so that it notifies the user when the provided string input cannot be converted to double. 我想编写一个自定义的setter,以便在提供的字符串输入不能转换为double时通知用户。 The error that I am getting is that value is a string and cannot be converted to double. 我得到的错误是值是一个字符串,不能转换为double。

public double Seasonalprop
        {
            get { return (double)GetValue(SeasonalProperty); }
            set
            {
                try
                  {
                          Double.TryParse(value, out parsedouble);
                          SetValue(SeasonalProperty, value);

                  }
                  catch(Exception ex)
                  {
                          MessageBox.Show(" String Input cannot be converted to 
                          type double");
                  }

            }
        }

I think you want to write a custom settor so that it notifies the user if the value in text box is invalid. 我认为您想编写一个自定义设置器,以便它在文本框中的值无效时通知用户。

Have a look at the docs for validation in WPF 看看在WPF中进行验证的文档

The text content of the TextBox in the following example is bound to the Age property (of type int) of a binding source object named ods. 在下面的示例中,TextBox的文本内容绑定到名为ods的绑定源对象的Age属性(int类型)。 The binding is set up to use a validation rule named AgeRangeRule so that if the user enters non-numeric characters or a value that is smaller than 21 or greater than 130, a red exclamation mark appears next to the text box and a tool tip with the error message appears when the user moves the mouse over the text box. 绑定设置为使用名为AgeRangeRule的验证规则,因此,如果用户输入非数字字符或小于21或大于130的值,则文本框旁边会出现一个红色的感叹号,并且工具提示带有当用户将鼠标移到文本框上时,会出现错误消息。

I think your whole concept was gone to a wrong direction. 我认为您的整个概念走错了方向。 First of all, when a binding expression updates a dependency property, it will call its onwer's SetValue method but not its clr property wrapper. 首先,当绑定表达式更新依赖项属性时,它将调用其onwer的SetValue方法,而不是其clr属性包装器。 So a custom setter would do nothing for you in this situation. 因此,在这种情况下,自定义设置器将对您无济于事。 And as @peeyushsingh's answer, wpf has binding validation for this. 作为@peeyushsingh的答案,wpf对此具有绑定验证。 So, something you need should be like: 因此,您需要的东西应该是:

        <TextBox Text="{Binding Seasonalprop, ValidatesOnExceptions=True}">
            <Validation.ErrorTemplate>
                <ControlTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border BorderThickness="1" BorderBrush="Red" >
                            <AdornedElementPlaceholder/>
                        </Border>
                        <TextBlock Foreground="Red" Margin="2" Name="cc"
                                   Text="!  Not a double."/>
                    </StackPanel>
                </ControlTemplate>
            </Validation.ErrorTemplate>
        </TextBox> 

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

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