数据绑定到计算字段
我遇到了一个小问题,我试图将 DataGrid 的 DataTextColumn 绑定到计算字段。
WPF
<DataGrid ItemsSource="{Binding Path=CurrentRoster, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
AlternatingRowBackground="Gainsboro"
AlternationCount="2">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Student Enrolled"
ItemsSource="{Binding Source={StaticResource AvailableStudents}}"
SelectedItemBinding="{Binding Path=Student}">
</DataGridComboBoxColumn>
<DataGridTextColumn Header="Registration" Binding="{Binding Path=RegistrationCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Lodging" Binding="{Binding Path=LodgingCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Travel" Binding="{Binding Path=TravelCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Dining" Binding="{Binding Path=DiningCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Total Costs" IsReadOnly="True" Binding="{Binding Path=TotalCosts, StringFormat='{}{0:C}'}"/>
</DataGrid.Columns>
其中 Student 是一个 Entity 对象,并添加了一小部分。 TotalCosts 不是数据库表上的字段,因此我为此创建了一个部分类。
public partial class Student
{
public Decimal TotalCosts
{
get { return (LodgingCosts + RegistrationCosts + TravelCosts + DiningCosts); }
}
}
我遇到的问题是,当您填写任何其他字段时,TotalCosts 不会自动更新。我的猜测是因为它没有被列为依赖属性。对于没有设置的属性,如何解决此问题?
I'm running into a small problem where I'm trying to bind a DataTextColumn of a DataGrid to a Calculated Field.
WPF
<DataGrid ItemsSource="{Binding Path=CurrentRoster, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
AlternatingRowBackground="Gainsboro"
AlternationCount="2">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Student Enrolled"
ItemsSource="{Binding Source={StaticResource AvailableStudents}}"
SelectedItemBinding="{Binding Path=Student}">
</DataGridComboBoxColumn>
<DataGridTextColumn Header="Registration" Binding="{Binding Path=RegistrationCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Lodging" Binding="{Binding Path=LodgingCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Travel" Binding="{Binding Path=TravelCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Dining" Binding="{Binding Path=DiningCosts, StringFormat='{}{0:C}'}"/>
<DataGridTextColumn Header="Total Costs" IsReadOnly="True" Binding="{Binding Path=TotalCosts, StringFormat='{}{0:C}'}"/>
</DataGrid.Columns>
Where Student is a Entity object with one small addition. TotalCosts isn't a field on the db tables, so I created a partial class for this.
public partial class Student
{
public Decimal TotalCosts
{
get { return (LodgingCosts + RegistrationCosts + TravelCosts + DiningCosts); }
}
}
The problem I'm experiencing is that TotalCosts is not automatically updating when you fill in any of the other fields. My guess it is because it is not listed as a dependency property. How do I resolve this for a property where there is no set ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我假设 Student 实现了 INotifyPropertyChanged。您所要做的就是注册 LodgingCosts + RegistrationCosts + TravelCosts + DiningCosts 的 PropertyChanged 事件,并引发 TotalCosts 的 PropertyChanged 事件。
I'm assuming Student implements INotifyPropertyChanged. what you have to do is to register to the PropertyChanged Event for LodgingCosts + RegistrationCosts + TravelCosts + DiningCosts, and raise the PropertyChanged event for TotalCosts.
您可以在
TotalCosts
依赖的每个属性的 setter 中调用OnPropertyChanged("TotalCosts")
,它将刷新绑定You can call
OnPropertyChanged("TotalCosts")
in the setters of each property thatTotalCosts
depends on, it will refresh the binding你好,我想我可以帮助你,我有 sqlServer 数据库的代码
我有一个数据源,其中有一个用于我的数据库的数据集
通过这个技巧,在我有一个值后,C 列会自动计算
在 A 列和 B 列中,例如,如果我在 A 列中输入 10,在 B 列中输入 5
然后我只需按 Tab 键移动到 C 列,值 2 就会自动出现
无需单击其他行或按 Enter 键。
ColumnC 是一个计算列,具有表达式 (ColumnaA/ColumnaB) 和类型
系统.十进制。
这是我的 xaml 代码:
Hello o think i can help you i have this code for a data base in sqlServer
and i have a Data source with a DataSet for my Data Base
With this trick the column C is calculated automatically after i have a value
in columns A and B for example if i put 10 in columna A and 5 in column B
then i just press Tab Key to move to the columnC and the value 2 appears automatically
no need to click other row or press enter.
ColumnC is a calculated column whit the expression (ColumnaA/ColumnaB) and of type
System.Decimal.
And there is my xaml code:
只需在为
TotalCosts
属性创建DataBinding
时,将DataSourceUpdateMode
更改为OnPropertyChanged
即可。Just change the
DataSourceUpdateMode
toOnPropertyChanged
when you createDataBinding
to theTotalCosts
property.您需要在类上实现
INotifyPropertyChanged
。以下是示例:XAML 代码:
测试:
在 2 个文本框中键入内容..然后在按钮上单击查看人员 P1 的值..u 将找到具有该值的计算字段..
希望它能帮助你..
谢谢,
哈维克
You need to implement the
INotifyPropertyChanged
on the class. Here is the Example:XAML code:
Test:
type something in the 2 textboxes..and on the Button click see the value of the person P1 ..u will find the calculated field with the value..
hope it helps u..
Thanks,
BHavik