自定义 ObservableCollection 属性的数据绑定问题
我正在制定一个小型个人理财计划,使用“信封”作为预算方法。我这样做主要是为了学习 WPF 和 WPF。 MVVM。我遇到了一个问题,我无法将数据绑定到我在自定义 ObservableCollection 中创建的自定义属性,如下所示:
public class ObservableEnvelopeCollection : ObservableCollection<Envelope>
{
public decimal Total
{
get
{
decimal total = 0;
foreach (Envelope env in this)
{
total += env.Balance;
}
return total;
}
}
public decimal SavingsTotal
{
get
{
blah blah. . .
}
}
}
我能够毫无问题地将我的数据网格数据绑定到集合,但是在我的数据网格下面,我有标签我需要显示集合中信封的余额总额。我能够对集合的 Count 属性进行数据绑定,该属性是父 ObservableCollection 类的属性,但无法对自定义类中的 Total 或 SavingsTotal 进行数据绑定。
当程序运行时,标签只是空白,如果我使用 VS2010 设计器并转到标签内容的小数据绑定向导,它会带有下划线的“Total”并显示工具提示:“路径项“Total”无法解决了。”同样,如果我将 Path 更改为 EnvColl.Count ,它就可以工作。
<Label Content="{Binding Path=EnvColl.Total}"/>
另请注意,在我的 ViewModel 中,我可以毫无问题地访问我的集合中的 Total 和 SavingsTotal 属性,只是在 XAML/数据绑定中它不起作用。
I'm making a small personal finance program that uses "envelopes" as the budgeting method. I'm mostly doing this as a means to learn WPF & MVVM. I've run into a problem where I can't databind to the custom properties I created in my custom ObservableCollection, seen here:
public class ObservableEnvelopeCollection : ObservableCollection<Envelope>
{
public decimal Total
{
get
{
decimal total = 0;
foreach (Envelope env in this)
{
total += env.Balance;
}
return total;
}
}
public decimal SavingsTotal
{
get
{
blah blah. . .
}
}
}
I'm able to databind my datagrid to the collection with no trouble, however below my datagrid I have labels that I need to display the total of balances for the envelopes in the collection. I'm able to databind to the Count property of the collection, which is a property of the parent ObservableCollection class, but I can't databind to Total or SavingsTotal from my custom class.
The label is just blank when the program is run, and if I use the VS2010 designer and go to the little databinding wizard for the label's Content, it has 'Total' underlined and shows the tooltip: "Path item 'Total' could not be resolved." Again, if I change Path to EnvColl.Count it works.
<Label Content="{Binding Path=EnvColl.Total}"/>
One more note, in my ViewModel I can access the Total and SavingsTotal properties in my collection with no problem, it's just in XAML/databinding that it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这是几年前发布的,但我遇到了类似的问题,我有一个包含
Label
和DataGrid
的Grid
。我能够将DataGrid
的ItemsSource
设置为ObservableCollection
,但无法访问Label 中所需的属性
。因此,我所做的是将包含Grid
的DataContext
设置为ObservableCollection
,然后使用RelativeSource
属性我的绑定来查找祖先(Grid
)。语法看起来像这样:我认为这实现了原始海报所寻求的。如果没有的话,希望它可以帮助其他一些人。
I know this was posted a couple of years ago, but I had a similar problem where I had a
Grid
which contained aLabel
and aDataGrid
. I was able to set theItemsSource
of myDataGrid
to anObservableCollection
, but I couldn't access the desired properties in myLabel
. So what I did was set theDataContext
of my containingGrid
to theObservableCollection
, then I used theRelativeSource
property of my binding to find the Ancestor (Grid
). The syntax looked something like this:I think that achieves what the original poster was looking for. And hopefully if not it can help some others.
vs2010
(一如既往,一些新用户可能需要了解这个问题。)
当我多次在调试模式下运行应用程序时,它发生在我身上,突然我必须杀死它/停止调试器,然后我注意到它不知道什么时候它必须重新编译代码。
我注意到,如果我关闭并重新打开应用程序/解决方案,会有很大帮助。如果您在调试模式下运行,那么您肯定必须经常这样做。
vs2010
(as always some new commers might need to know about this issue.)
it happened to me when I ran the app on debug mode many times and suddently I have to kill it/stop debugger, then I notice that it does not know when it has to recompile code.
I notice that if I close and reopen the app/solution that helps a lot. if you are running in debugging mode definitely you have to do this every so often.
呸!我讨厌发生这样的事情......我去每个 Kent 添加 BindsDirectlyToSource=true 并将其添加到错误的位置,导致构建错误。然后我将其删除并重建,数据绑定突然开始工作。 。 。 :\ 我已经多次重建解决方案,但没有运气,但导致编译错误,然后删除它使事情开始工作。想知道 2010 的编译器是否有问题......?
Bah! I hate when things like this happen... I went to add BindsDirectlyToSource=true per Kent and added it to the wrong place, causing a build error. I then removed it and rebuilt, and the databinding suddenly started working. . . :\ I had rebuilt the solution several times with no luck, but causing a compile error then removing it made things start working. Wonder if there's something buggy in 2010's compiler...?