【问题标题】:How can I bind multiple ListViews within a single template?如何在单个模板中绑定多个 ListView?
【发布时间】:2011-11-02 08:29:08
【问题描述】:

我有一个用于 3 个 ListView 的 ControlTemplate。 我在 ListView 上使用模板,例如。

Template="{StaticResource listViewTemplate}"

当我尝试将 Collection 绑定到列表视图 ItemsSource 时,什么也没有发生。 如果我绑定 ControlTemplate 的 Collection ItemsSource 它工作正常。

问题是我有 3 个使用此 ControlTemplate 的 ListView,我想将 3 个不同的集合绑定到 ListView。

这可能吗?

【问题讨论】:

  • 你能添加更多不适合你的 xaml 吗?
  • 需要更多信息,向我们展示您的模板和整个 List 声明,以及您的 ItemSource 是什么类型 - ObervableCollection List、Dictionary?
  • 在这种情况下,通常最好创建一个原型,尝试用最少的标记和代码来做你想做的事情。你不仅可以用它来提出一个很好的问题,而且还可以在你提出问题之前解决你的问题。

标签: wpf mvvm controltemplate


【解决方案1】:

您的控件模板需要使用 TemplateBinding 例如绑定其 ItemsSource

<ControlTemplate ...>
    <ItemsSource = "{templateBinding ItemsSource}"

这将使您能够在列表视图 ItemSource 上绑定一个集合(这将传递给 ControlTemplates ItemsSource)

【讨论】:

    【解决方案2】:

    x:Shared="False" 添加到您的静态资源中。这将确保每次请求资源时都会生成对象的浅表副本。

    这样,您的每个 ListView 都会获得不同的控件模板实例。

    这是关于它的文档:http://msdn.microsoft.com/en-us/library/aa970778.aspx

    【讨论】:

      猜你喜欢
      • 2011-06-22
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多