wpf数据绑定中的问题
我有一个与int属性ProductPrice绑定的文本字段和按钮,如果我在文本框中输入了一些错误的数据(例如,尝试设置的编号wpf数据绑定中的问题,wpf,exception,data-binding,properties,onchange,Wpf,Exception,Data Binding,Properties,Onchange,我有一个与int属性ProductPrice绑定的文本字段和按钮,如果我在文本框中输入了一些错误的数据(例如,尝试设置的编号{binding ProductPrice Mode=TwoWay} 顺便说一句,引发了哪个异常 编辑:回答评论 我看到您有两个选择: 介绍string ProductPriceValue属性(在封面下使用int类型的原始属性),并将文本框绑定到该属性 实现IValueConverter并在绑定中使用它 只是想知道为什么您没有为ProductPrice属性使用double类
{binding ProductPrice Mode=TwoWay}
顺便说一句,引发了哪个异常
编辑:回答评论
我看到您有两个选择:
string ProductPriceValue
属性(在封面下使用int
类型的原始属性),并将文本框绑定到该属性IValueConverter
并在绑定中使用它只是想知道为什么您没有为
ProductPrice
属性使用double
类型尝试设置{Binding ProductPrice Mode=TwoWay}
顺便说一句,引发了哪个异常
编辑:回答评论
我看到您有两个选择:
string ProductPriceValue
属性(在封面下使用int
类型的原始属性),并将文本框绑定到该属性IValueConverter
并在绑定中使用它只是想知道为什么您没有为
ProductPrice
property使用double
type在“AddProductCommand”的“CanExecute”中会发生什么情况?private bool CanAddNewProduct(){if(this.Product!=null&&this.Product.IsValid&&this.IsAddProductView&&this.ProductList!=null&&this.ProductList.Contains(this.Product,new Comparer()){return true;}return false;}
假设您在产品类中实现IDataErrorInfo,首先在ProductPrice属性设置器中放置一个调试指针,查看在UI文本框中输入一些文本时是否达到该值,然后在CanExecute代码中放置一个调试指针,检查是否返回true\false,然后调试您的this[string columnName]属性是否引发错误消息。如果我在文本框中输入了错误的数据(某些文本),setter将无法工作,因为在此之前生成了异常。在“AddProductCommand”的“CanExecute”中会发生什么情况?private bool CanAddNewProduct(){If(this.Product!=null&&this.Product.IsValid&&this.IsAddProductView&&this.ProductList!=null&&this.ProductList.Contains(this.Product,new Comparer()){return true;}return false;}
假设您在产品类中实现IDataErrorInfo,首先在ProductPrice属性设置器中放置一个调试指针,查看在UI文本框中输入一些文本时是否达到该值,然后在CanExecute代码中放置一个调试指针,检查是否返回true\false,然后调试您的this[string columnName]属性是否引发错误消息。如果我在文本框中输入了错误的数据(某些文本),setter将无法工作,因为在此之前生成了异常。Mode=TwoWay
没有帮助。错误:System.Windows.data错误:7:ConvertBack无法转换值“r”(类型“String”)。BindingExpression:Path=ProductPrice;DataItem='ProductViewModel'(HashCode=41182536);目标元素为“TextBox”(名称=“”);目标属性为“Text”(类型为“String”)FormatException:'System.FormatException:输入字符串的格式不正确。
ProductViewModel.ProductPrice是字符串类型?不,是int。我知道。但接下来的想法是:用户在文本字段中输入一些数字-正常,按钮将被启用,但如果用户输入错误的数据-按钮将被禁用。感谢您的好主意:)我创建了string属性并将其与TextBox绑定。之后,我可以像这样控制异常:公共字符串ProductPriceValue{get{返回this.Model.ProductPrice.ToString();}set{try{this.Model.ProductPrice=Int32.Parse(value);}catch{this.Model.ProductPrice=0;}}}
@Roman:很高兴听到这个消息。我建议您使用Int32.TryParse而不是Parse,这样您就可以避免在文本框中输入错误数字时出现异常,并且不需要昂贵的try-catch-blockMode=TwoWay
没有帮助。错误:System.Windows.Data错误:7:ConvertBack无法转换值“r”BindingExpression:Path=ProductPrice;DataItem='ProductViewModel'(HashCode=41182536);目标元素为'TextBox'(Name='');目标属性为'Text'(类型为'String')FormatException:'System.FormatException:输入字符串的格式不正确。
ProductViewModel.ProductPrice是字符串类型?不,是int。我知道。但接下来的想法是:用户在文本字段中输入一些数字-正常,按钮将被启用,但如果用户输入错误的数据-按钮将被禁用。感谢您的好主意:)我创建了string属性并将其与TextBox绑定。之后,我可以像这样控制异常:公共字符串ProductPriceValue{get{返回this.Model.ProductPrice.ToString();}set{try{this.Model.ProductPrice=Int32.Parse(value);}catch{this.Model.ProductPrice=0;}}}
@Roman:很高兴听到这个消息。我还建议您使用Int32.TryParse而不是Parse,这样您就可以避免在文本框中输入错误数字时出现异常,而不需要昂贵的try-catch块
<TextBox Grid.Row="4" Grid.Column="2"
Text="{Binding ProductPrice, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}"/>
<Button HorizontalAlignment="Right" Command="{Binding Path=DataContext.AddProductCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Margin="2" Width="60" Grid.Row="6" Grid.Column="2" Content="Add" />