自定义 ObservableCollection 属性的数据绑定问题

发布于 2024-09-12 12:32:37 字数 1040 浏览 7 评论 0原文

我正在制定一个小型个人理财计划,使用“信封”作为预算方法。我这样做主要是为了学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-09-19 12:32:58

我知道这是几年前发布的,但我遇到了类似的问题,我有一个包含 LabelDataGridGrid 。我能够将 DataGridItemsSource 设置为 ObservableCollection,但无法访问 Label 中所需的属性。因此,我所做的是将包含 GridDataContext 设置为 ObservableCollection,然后使用 RelativeSource 属性我的绑定来查找祖先(Grid)。语法看起来像这样:

<Grid DataContext = "{Binding MyObservableCollection}">
    <Label Content = "{Binding MyDesiredProperty}"/>
    <DataGrid ItemsSource = "{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestoryType={x:Type Grid}}}">
    </DataGrid>
</Grid>

我认为这实现了原始海报所寻求的。如果没有的话,希望它可以帮助其他一些人。

I know this was posted a couple of years ago, but I had a similar problem where I had a Grid which contained a Label and a DataGrid. I was able to set the ItemsSource of my DataGrid to an ObservableCollection, but I couldn't access the desired properties in my Label. So what I did was set the DataContext of my containing Grid to the ObservableCollection, then I used the RelativeSource property of my binding to find the Ancestor (Grid). The syntax looked something like this:

<Grid DataContext = "{Binding MyObservableCollection}">
    <Label Content = "{Binding MyDesiredProperty}"/>
    <DataGrid ItemsSource = "{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestoryType={x:Type Grid}}}">
    </DataGrid>
</Grid>

I think that achieves what the original poster was looking for. And hopefully if not it can help some others.

泪冰清 2024-09-19 12:32:55

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.

溺孤伤于心 2024-09-19 12:32:52

呸!我讨厌发生这样的事情......我去每个 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...?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文