【问题标题】:Switching a binding with a binding用绑定切换绑定
【发布时间】:2011-12-13 17:39:01
【问题描述】:

我有一个 BlogStore 类,它包含两个像这样的 observablecollections

public class BlogStore {
    public ObservableCollection<Blog> blogs ...

    public ObservableCollection<Blog> favourites ...
}

public BlogStore blogStore ...

不,我想重用执行以下绑定的控件

ItemsSource="{Binding blogStore.blogs}

这样我就可以切换到favourites,以下不起作用,但我想要类似的东西。

ItemsSource={Binding blogStore{Binding category, ElementName=blogControl}

并且在我后面的控件代码中会有一个依赖属性。

也许转换器可以解决问题?

【问题讨论】:

    标签: silverlight windows-phone-7 data-binding


    【解决方案1】:

    如果您将 BlogStore 视为 ViewModel,那么它将公开一些其他属性。

    Category 绑定到您用于选择要显示的类别的任何控件。

    还有一个CategoryBlogs 属性,根据Category 的值返回blogsfavourites 的值。

    您将实现INotifyPropertyChanged,因此您将确保在更改Category 属性时为“CategoryBlogs”触发PropertyChanged 事件。

    你会将ItemsSource 绑定到CategoryBlogs

    【讨论】:

    • 如何将参数传递给 ViewModel?我可以将属性作为 ViewModel 添加到 blogStore 吗?
    • 我通过添加一个名为 Category 的 DependencyProperty 和一个名为 related 的属性来解决这个问题,它的 getter 根据依赖属性值返回博客或收藏夹。
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多