简体   繁体   English

WPF列表中的“静态”绑定

[英]WPF “static” binding in a List

I have a problem with a binding in a List. 我在列表中绑定有问题。

I have a List of objects. 我有一个对象列表。 This List is bound to a ListBox. 此列表绑定到列表框。 Of every object in my List I can open a ContextMenu: 我可以在列表中的每个对象中打开一个ContextMenu:

<ListBox.ItemContainerStyle>
  <Style TargetType="ListBoxItem">
    <Setter Property="ContextMenu">
      <Setter.Value>
        <ContextMenu>
          <MenuItem Header="First"  IsEnabled="{Binding FirstEnabled}"/>
          <MenuItem Header="Second"  IsEnabled="{Binding SecondEnable}"/> 
        </ContextMenu>
      </Setter.Value>
    </Setter>
  </Style>
</ListBox.ItemContainerStyle>

In the code like this my objects in the list having the two booleans to bind. 在这样的代码中,列表中的我的对象具有两个要绑定的布尔值。 Now I want to bind this two booleans not to the objects. 现在,我不想将这两个布尔值绑定到对象。 I want to bind it "static" to my DataContext. 我想将其“静态”绑定到我的DataContext。 This is not working like this and I have no idea how to realize it. 这不是这样工作,我也不知道如何实现。

I googled a lot but found nothing helpful ... 我在Google上搜索了很多,但没有发现任何帮助...

Thanks for helping! 感谢您的帮助!

Since, ContextMenu applies to ListBoxItem it will have its DataContext value and ListBoxItem will be its PlacementTarget . 由于ContextMenu应用于ListBoxItem因此它将具有其DataContext值,而ListBoxItem将是其PlacementTarget So if you want to bind to property of ListBox.DataContext you need to pass current ListBox.DataContext as, for example, Tag to ListBoxItem and then you need to refer to it from ContextMenu via its PlacementTarget . 因此,如果要绑定到ListBox.DataContext属性,则需要将当前ListBox.DataContext作为例如Tag传递给ListBoxItem ,然后需要通过ContextMenu通过其PlacementTarget引用它。 It's all because ContextMenu uses Popup which creates its own visual tree so simple RelativeSource / ElementName binding won't work 这是因为ContextMenu使用Popup 创建了自己的可视化树,因此简单的RelativeSource / ElementName绑定不起作用

<Style TargetType="ListBoxItem">
    <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=DataContext}"/>
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="First"  IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.FirstEnabled}"/>
                <MenuItem Header="Second" IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.SecondEnable}"/>                    
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

You can reference every datacontext with the ElementName Syntax: 您可以使用ElementName语法引用每个数据上下文:

<ListBox x:Name="myListBox">
    <ListBox.ItemContainerStyle>
       <Style TargetType="ListBoxItem">
           <Setter Property="ContextMenu">
               <Setter.Value>
                   <ContextMenu>
                       <MenuItem Header="First"  IsEnabled="{Binding Path=DataContext.FirstEnabled, ElementName=myListBox}"/>
                       <MenuItem Header="Second"  IsEnabled="{Binding Path=DataContext.SecondEnable, ElementName=myListBox}"/> 
                  </ContextMenu>
               </Setter.Value>
           </Setter>
       </Style>
   </ListBox.ItemContainerStyle>
</ListBox>

With this syntax you use the DataContext of your ListBox and not from the ListItem. 使用此语法,您可以使用ListBox的DataContext,而不是使用ListItem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM