从 DataTemplate 内的资源绑定

发布于 2024-10-25 04:20:50 字数 1810 浏览 3 评论 0原文

有没有某种方法可以获取 DataTemplateDataContext 以在其资源内的绑定中使用?

<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>

上述模板用作ListBoxCellTemplate。该级别的对象有两个属性,History(包含“历史信息”对象的列表)和Model(包含一堆其他内容,包括Type )。我使用 ItemsControl 来显示彼此相邻的历史项目;我想为每一个显示一张图像,该图像是从BitmapProvider获取的,它是一个IValueConverter

转换器需要两位信息才能获得结果:一位是各个历史项目的Data,另一位是整个集合的Type。一个额外的复杂性是构建这个特定的转换器(或更改给定的Type)的成本很高,所以我不想将它放在单个历史项目的级别,或者使用MultiBinding,我无法将其放在模板之外,因为那样它就无法访问Type

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

System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。 BindingExpression:Path=模型.类型;数据项=空;目标元素是“BitmapProvider”(HashCode=57142809); target 属性是 'Type' (type 'TypeDetails')

我理解这意味着资源无法弄清楚如何获取它所包含的元素的 DataContext

(我已经搜索过,我能找到的大多数答案都建议将其移到模板之外或使用 MultiBinding 代替——据我所知,在这种情况下,这两种方法都不会真正起作用,正如我上面所解释的,但我很高兴被证明是错误的,或者给出另一种选择。)

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>

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.

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 Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Model.Type; DataItem=null; target element is 'BitmapProvider' (HashCode=57142809); target property is 'Type' (type 'TypeDetails')

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

(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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

就像说晚安 2024-11-01 04:20:50

我认为您可以使用 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>

I think you can accomplish that with DataContextSpy.

try something like:

<ItemsControl.Resources>
  <spy:DataContextSpy x:Key="Spy"/>
  <app:BitmapProvider x:Key="Converter" ShowDetails="True"
                      Type="{Binding DataContext.Model.Type,Source={StaticResource Spy}}" />
</ItemsControl.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文