ItemTemplate 中 DataContext 的访问属性 [英] Access property of DataContext inside ItemTemplate

查看:35
本文介绍了ItemTemplate 中 DataContext 的访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常讨厌的绑定问题.我知道还有其他关于将 itemtemplate 内的项目绑定到模板外对象的 datacontext 的主题.但是,这不起作用,即第一个文本块根据需要显示测试",而 itemtemplate 内的相同文本框什么也不显示.

I have a really nasty problem with bindings. I know that there are other topics regarding binding itmes inside itemtemplate to datacontext of an object outside the template. However, this just won't work, i.e. the first textblock display 'Test' as desired whereas the same textbox inside the itemtemplate shows nothing.

  <TextBlock Text="{Binding DataContext.Test, ElementName=myList}"/>
  <ItemsControl x:Name="myList" ItemsSource="{Binding AllItems}"
                Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Center">
       <ItemsControl.ItemsPanel>
           <ItemsPanelTemplate>
                <toolkit:WrapPanel Orientation="Horizontal"                                    
                                           ItemHeight="170" ItemWidth="140"/>
           </ItemsPanelTemplate>
       </ItemsControl.ItemsPanel>
       <ItemsControl.ItemTemplate>
           <DataTemplate>
              <StackPanel>
                 <Image x:Name="{Binding KeyName}"
                        Source="{Binding ImagePath}"
                        Width="128"
                        Height="128">
                 </Image>

                 <TextBlock Text="{Binding DataContext.Test, ElementName=myList}"/>
                        </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我很感激这里的人们的帮助,因为这对我来说真的是一个问题.

I would appreciate some help here folks as this is really a problem for me.

推荐答案

在 itemtemplate 内部,绑定被初始化为 AllItems 中当前 item 的上下文.

Inside the itemtemplate, the binding is initialized to the context of the current item in AllItems.

更新

ItemTemplate 之外,您的绑定与页面的 DataContext 相关.**

Outside of the ItemTemplateyour bindings are relative to the DataContext of the page.**

一旦在 ItemTemplate 中,绑定就会被限制在当时被评估的项目的范围内.

Once inside an ItemTemplate then bindings are limited to the scope of the item specifically being evaluated at that time.

因此,如果我们假设以下内容(基于您问题中的代码):

So, if we assume the following (based on the code in your question):

<ItemsControl x:Name="myList" ItemsSource="{Binding AllItems}" >
    <ItemsControl.ItemTemplate>
         <DataTemplate>
             <StackPanel>
                 <TextBlock x:Name="tb1"
                        Text="{Binding DataContext.Test, ElementName=myList}"/>
                 <TextBlock x:Name="tb2" Text="{Binding KeyName}"/>
             </StackPanel>
         </DataTemplate>
     </ItemsControl.ItemTemplate>
 </ItemsControl>

tb1 无法直接访问 DataContext 对象.
tb2 不能访问 KeyName - 假设任何对象 AllItems 是一个 IEnumerable 包含具有该名称的属性.

tb1 cannot access the DataContext object directly.
tb2 cann access KeyName - assuming that whatever object AllItems is an IEnumerable of contains a property with that name.

据我所知,在 itemtemplate 中,从枚举过去的项目控制着绑定源,这不能被覆盖(通过设置 ElementName 或其他方式).

As I understand it, inside an itemtemplate, the item past from the enumeration controls the binding source and this can't be overridden (by setting ElementName or otherwise).

如果枚举中的每个对象都需要来自 Test 的值,则需要将其添加为枚举中对象的属性.

If you need the value from Test in every object in your enumeration then you'll need to add it as a property of the object in the enumeration.

我相信有比我更博学的人可以解释为什么会这样或给出更好的解释,但这就是要点.

** 假设没有其他嵌套的 ItemsControls(或等效项)

** Assuming no other nesting of ItemsControls (or equivalent)

这篇关于ItemTemplate 中 DataContext 的访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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