wpf绑定显示两位小数

wpf绑定显示两位小数,第1张

关于c#:在WPF绑定中设置小数属性的默认值

c#mvvmwpf

Setting the Default Value for Decimal Property in WPF Binding

我将WPF表单绑定到类的Decimal属性。 如果用户输入无效的格式(字符串而不是十进制),则文本框会自动以红色显示。 但是,我想通过在将插入的数据存储到数据库之前进行验证来使其更加安全。

问题是,每当用户输入非十进制值时,绑定将返回0,而不是null或error。 因此,它无需第二级验证就可以进入数据库。

验证WPF与小数的绑定的最佳方法是什么? 现在它不会返回null,所以我没有任何办法捕获错误。

这是我绑定文本框的方式

1

        <TextBox x:Name="stockTxtBx" Grid.Row="3" Grid.Column="1"  Style="{StaticResource StandardBox}" Text="{Binding StockOnHand}"/>

另外,在哪里可以修改以添加验证?

相关讨论

我确定此页面会为您提供帮助

@HaikalNashiha也许谷歌翻译

@HaikalNashiha此页面也很适合您

The problem is, whenever a user enter a non decimal value, the binding will return 0 instead of null or error

您在上述声明中略有错误。当用户输入某些文本的特定字段的类型无效时,实际发生的情况是:

无效的文本会导致TextBox周围出现红色边框(或其他修饰,具体取决于ErrorTemplate值)。

数据绑定属性值保留为最后输入的有效值

因此,在您的情况下,最后一个有效值可能是0,这就是为什么您认为无效值将始终返回0的原因。因此,实际上,只有无效值被忽略,而最后一个有效值仍然保留。

但是,要解决此问题,您有几种选择。一种方法是在保存数据之前检查Validation.HasError附加属性的值。显然,如果您检测到存在任何错误,那么您将d出一条消息来提醒用户,而不是继续保存。您可以从MVVM问题中的Binding Validation.HasError属性中找到有关此方法的更多信息。

另一个选择是限制特定TextBox的文本输入,这样就不可能输入非数字键。在这里,我不会再详细说明如何执行此 *** 作,而是希望您在Stack Overflow上要求您查看

WPF中提供了数据绑定的功能, *** 作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面 *** 作一下下拉列表框控件ComboBox控件的数据绑定 *** 作。

要绑定到ComboBox控件的自定义类:

public class LocationRoad

{

public int ID { setget}

public string Code { setget}

public string Info { setget}

}

建立数据源,就将此数据集合当作数据源绑定到ComboBox:

///

/// 当ComboBox选中项更改时发生

///

private LocationRoad _selectLocation

public LocationRoad SelectLocation

{

get

{

return this._selectLocation

}

set

{

this._selectLocation = value

if (this.PropertyChanged != null)

PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation"))

}

}

private ObservableCollection _locationRoad = null

public ObservableCollection LocationSource

{

get

{

if (this._locationRoad == null)

{

this._locationRoad = new ObservableCollection() {

new LocationRoad() { ID = 1, Code = "NGQ", Info = "南岗区" },

new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里区" },

new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外区" },

new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房区" },

new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊区" },

}

}

return this._locationRoad

}

set

{

this._locationRoad = value

if (this.PropertyChanged != null)

PropertyChanged(this, new PropertyChangedEventArgs("LocationSource"))

}

}

将AutoGenerateColumns设置为false。然后在代码中手动生成不同表相对应的百分比列宽的与数据库表的列名对应的列。其余部分不变即可。

代码:

string attr = ""

dataDataGrid.AutoGenerateColumns = false

MultiSecurity ms = new MultiSecurity()

OracleConnection oraCon = ms.CreateConnection("msdb", "maple", "manager")

foreach (string att in ms.getAttributes(tablename, oraCon)) //获取指定表的所有列

{

attr += att + ","

dataDataGrid.Columns.Add(new DataGridTextColumn() { Header = att, Binding = new Binding(att), Width = new DataGridLength(2, DataGridLengthUnitType.Star) }) //为DataGrid生成百分比列宽的列,相当于xaml中设置Width = "2*"

}

attr = attr.Substring(0, attr.Length - 1)

sql = "select "+attr+" from " + node.Name //生成查询指定列的sql语句

DataTable dt = ms.getData(sql, oraCon)

dataDataGrid.ItemsSource = dt.DefaultView //将结果集绑定到DataGrid


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9886431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存