DataGrid Items集合并不总是刷新

时间:2015-02-12 20:48:32

标签: wpf binding datagrid

我有DataGrid绑定到ObservableCollection<Client>。 我有一个UserControl负责从Textbox值对集合应用过滤器,如下所示:

private void UCFilterBox_SearchTextChanged(object sender, string e)
{
    var coll = CollectionViewSource.GetDefaultView(dgClients.ItemsSource);
    coll.Filter = o =>
    {
        var c = o as Client;
        if (c != null)
        {
            bool ret =  (the filter...)

            return ret;
        }
        else
        {
            return false;
        }
    };
}

然后我有一个TextBlock,它绑定到DataGrid的{​​{1}}集合,如下所示:

Items

此操作正常,每次过滤<StackPanel Grid.Row="0" Margin="215,0,0,5" HorizontalAlignment="Left" VerticalAlignment="Bottom" Orientation="Horizontal"> <TextBlock Style="{StaticResource SmallTextBlockStyle}" Text="{Binding ElementName=dgClients, Path=Items.Count}" /> <TextBlock Style="{StaticResource SmallTextBlockStyle}" Text="{Binding ElementName=dgClients, Path=Items.Count, Converter={StaticResource ClientSingleOrPluralConverter}, StringFormat={} {0}}" /> </StackPanel> 时,值都会相应更改。

但是,我有另一个DataGrid绑定到TextBlock的{​​{1}}集合,它负责显示所显示数据的总和,而且这个数据不会更新!< / p>

DataGrid

Items仅在<TextBlock Margin="5" FontWeight="Bold" Text="{Binding ElementName=dgClients, Path=Items, Converter={StaticResource CalculateSumConvertor}, StringFormat={}{0:C}}" /> 的绑定时被点击一次,然后不再被点击。

这是转换器:

CalculateSumConvertor

我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

将绑定更改为以下内容并将转换器更改为IMultiValueConverter

    <TextBlock Margin="5"
       FontWeight="Bold">
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource CalculateSumConvertor}" StringFormat="{}{0:C}">
                <Binding ElementName="dgClients" Path="Items" />
                <Binding ElementName="dgClients" Path="Items.Count" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>

转换器:

class CalculateSumConvertor : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var clients = values[0] as ItemCollection;
        ...
    }




}