从资源绑定一个DataTemplate内 [英] Binding from resources inside a DataTemplate

查看:164
本文介绍了从资源绑定一个DataTemplate内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些办法让的DataContext 的DataTemplate 在其资源范围内绑定使用?

Is there some way to get the DataContext of a DataTemplate to use in bindings within its resources?

<DataTemplate x:Key="History">
  <ItemsControl ItemsSource="{Binding History}">
    <ItemsControl.Resources>
      <app:BitmapProvider x:Key="Converter" ShowDetails="True"
                          Type="{Binding Model.Type}" />
    </ItemsControl.Resources>
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Image Source="{Binding Data, Converter={StaticResource Converter}}" />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>

上面的模板用作 CellTemplate A 的ListBox 的。在该级别的对象有两个属性,历史(载历史信息对象的列表)和模式(包含一堆其他的东西,包括键入)。我使用的是的ItemsControl 显示历史悠久的项目相邻;我要为每一个显示图像,并且图像从 BitmapProvider ,这是一个的IValueConverter 获得。

The above template is used as the CellTemplate of a ListBox. The object at that level has two properties, History (containing a list of "historic info" objects) and Model (containing a bunch of other stuff, including Type). I'm using an ItemsControl to display the historic items next to each other; I want to display an image for each one, and the image is obtained from the BitmapProvider, which is an IValueConverter.

该转换器需要信息的两位获得的结果:一是个人历史项目的数据,另一个是类型全收的。另外一个复杂之处在于构建这个特定的转换器(或更改键入给它)是昂贵的,所以我不想把它在个人历史水平项目,或者使用 MultiBinding ,我不能把它的模板外,因为那么它不会有机会获得键入

The converter needs two bits of info to obtain a result: one is the Data of the individual historic items, and the other is the Type of the whole collection. An added complication is that constructing this particular converter (or changing the Type given to it) is expensive, so I don't want to put it at the level of the individual history item, or to use a MultiBinding, and I can't put it outside of the template because then it won't have access to the Type.

不幸的是,上面给了我以下错误:

Unfortunately, the above gives me the following error:

System.Windows.Data错误:2:无法找到目标元素理事FrameworkElement的或FrameworkContentElement上。 BindingEx pression:路径= Model.Type;的DataItem = NULL;目标元素是'BitmapProvider(哈希code = 57142809); target属性是'类型'(类型'TypeDetails')

我的理解意味着资源无法弄清楚如何获得的DataContext 它其中包含的元素。

Which I understand to mean that the resource can't figure out how to get the DataContext of the element it's contained within.

(我已搜查,大多数我能找到建议的答案移动它的模板之外或使用 MultiBinding 代替 - 两者都不将在此真的管用情况下,据我所知道的,正如我上面的解释。但我很高兴能证明是错误的,或者给出另一种选择。)

(I have searched, and most of the answers I could find suggested moving it outside the template or using a MultiBinding instead -- neither of which would really work in this case, as far as I can tell, as I've explained above. But I'd be delighted to be proven wrong, or given another alternative.)

推荐答案

我认为你可以做到与的 DataContextSpy

I think you can accomplish that with DataContextSpy.

尝试这样的:

<ItemsControl.Resources>
  <spy:DataContextSpy x:Key="Spy"/>
  <app:BitmapProvider x:Key="Converter" ShowDetails="True"
                      Type="{Binding DataContext.Model.Type,Source={StaticResource Spy}}" />
</ItemsControl.Resources>

这篇关于从资源绑定一个DataTemplate内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆