简体   繁体   English

在MultiBinding中使用另一个元素的ItemsSource属性

[英]Use another element's ItemsSource property in a MultiBinding

I'd like to use the ItemsSource property from a particular element as one of the bindings in another element's MultiBinding . 我想将特定元素的ItemsSource属性用作另一个元素的MultiBinding中的绑定之一。 Here's what I have so far: 这是我到目前为止的内容:

<Label>
  <Label.Content>
    <MultiBinding Converter="{converters:myMultiValueConverter}">
      <Binding Path="PageIndex" />
      <Binding ElementName="anotherElement" Path="ItemsSource"/>
    </MultiBinding>
  </Label.Content> 
</Label>

This works once (when the ItemsSource is initially set), but the binding fails to update when the ObservableCollection bound to the original element's ItemsSource property has items added or removed. 这可以工作一次(最初设置ItemsSource时),但是当绑定到原始元素的ItemsSource属性的ObservableCollection具有添加或删除的项目时,绑定将无法更新。 Is this kind of binding possible? 这种绑定可能吗?

Add a dummy binding (- you don't need the value -) like this to force the MultiBinding to be reevaluated: 像这样添加一个虚拟绑定(-您不需要值-)以强制重新评估MultiBinding

<Binding ElementName="anotherElement" Path="ItemsSource.Count"/>

Edit: Just noticed a flaw: If you move items that would not register if that does not change the Count property in-between, maybe this is relevant for you. 编辑:刚注意到一个缺陷:如果您移动不会注册的项目,并且这之间没有更改Count属性,那么这可能与您有关。 In that case you could bind to your own dummy for which you can fire change notifications upon CollectionChanged (not all that clean in any case though). 在那种情况下,您可以绑定到您自己的虚拟对象,您可以为该虚拟对象在CollectionChanged触发更改通知(尽管在任何情况下都不是很干净的)。

You might want to consider HighCore's suggestion, a get -only property that returns the calculated value for which you manually fire PropertyChanged in all places that it depends on is usually quite convenient. 您可能需要考虑HighCore的建议,一个get only属性返回相当大的方便,该属性返回计算值,您可以在该计算值的所有位置手动触发PropertyChanged

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

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