2

ObservableCollection在 ViewModel 中有一个静态 MarketList,它以Table下列方式绑定:

<FlowDocumentScrollViewer Height="216" VerticalAlignment="Top" Margin="-7,2,7,0" >
    <FlowDocument>
        <Table CellSpacing="0" Name="MBPTable"  >
            <Table.DataContext>
                <MultiBinding  UpdateSourceTrigger="Explicit"  Mode="TwoWay" Converter="{StaticResource indexerConverter}">
                    <Binding Path="MarketList" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" Mode="TwoWay" BindsDirectlyToSource="True"  />
                    <Binding Path="MBPindex"  Mode="TwoWay"   />
                </MultiBinding>
            </Table.DataContext>
        <Table.Resources>
            <local:IndexerConverter x:Key="indexerConverter"></local:IndexerConverter>
        </Table.Resources>

Table包含ListView绑定到 MarketList 的属性。

<ListView  Name="MarketByPriceList" Width="300" ItemsSource="{Binding MarketByPriceList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" AlternationCount="2" Margin="0,15,0,0" >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Orders"  Width="48"  DisplayMemberBinding="{Binding MBP_NoofBuy_Orders, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ></GridViewColumn>
            <GridViewColumn Header="Bid Qty" Width="48"  DisplayMemberBinding="{Binding MBPBID_Qty,  UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ></GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

这是转换器方法

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values != null && values[0] != DependencyProperty.UnsetValue)
        {
           // ObservableCollection<GlobalItems.Model.MarketWatchModel> allNames = (ObservableCollection<GlobalItems.Model.MarketWatchModel>)values[0];
            int index = (int)values[1];

            return GlobalItems.ViewModel.MarketWatchModelView.MarketList[index];
        }
        else
        {
            return null;
        }
    }

绑定工作正常,但集合中的更新未反映在 UI 中

4

2 回答 2

9

这似乎归结为此处描述的相同问题,并且基本上相同的解决方案应该有效。

更新(添加或删除) ObservableCollection 不会导致 MultiBinding 刷新,因为 MultiBinding 正在侦听 PropertyChanged 事件,更新 ObservableCollection 只会触发 CollectionChanged 事件。一个干净的解决方法是将绑定添加到 MultiBinding 中 ObservableCollection 的 Count 属性。例如

<MultiBinding  UpdateSourceTrigger="Explicit"  Mode="TwoWay" Converter="{StaticResource indexerConverter}">
  <Binding Path="MarketList" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" Mode="TwoWay" BindsDirectlyToSource="True"  />
  <Binding Path="MBPindex"  Mode="TwoWay"   />
  <Binding Path="MarketList.Count" /> <!-- Additional Binding causes refresh when MarketList is updated -->
</MultiBinding>

在所有现有绑定之后添加附加绑定,以便 MultiValueConverter 可以忽略额外值。当 ObservableCollection 的 Count 属性发生变化时(当您添加或删除项目时),它将触发 PropertyChanged 事件并且您的 MultiBinding 将刷新。

于 2014-04-16T19:04:58.333 回答
5

我正在解决同样的问题,但不仅需要在计数更改时触发它,还需要在修改项目时触发它。Dan 的答案和他链接的答案都让我指出了正确的方向,但在此过程中,我发现绑定到 Count 属性有更好的选择。而是绑定到“Item[]”,这将在添加或删除项目(与 Count 相同)以及重新排列或更改项目时触发 PropertyChange。

Dan 示例的更新版本:

<MultiBinding  UpdateSourceTrigger="Explicit"  Mode="TwoWay" Converter="{StaticResource indexerConverter}">
  <Binding Path="MarketList" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" Mode="TwoWay" BindsDirectlyToSource="True"  />
  <Binding Path="MBPindex"  Mode="TwoWay"   />
  <Binding Path="MarketList.Item[]" /> <!-- This is the difference -->
</MultiBinding>

我的来源是 ObservableCollection 的源代码,可在此处找到:http ://referencesource.microsoft.com/#System/compmod/system/collections/objectmodel/observablecollection.cs

于 2015-02-09T10:51:23.520 回答