0

所以,我有一个带有 TextBlock 的 DataGrid,它显示网格中两个文本框的聚合值。我通过使用值转换器绑定来做到这一点。这适用于负载,但我需要它在更改其聚合的其他实体的值时进行更新。这是我的一些代码:

这是绑定到视图的 ViewModel 中的我的 PagedCollectionView。

private PagedCollectionView _grievances;
public PagedCollectionView Grievances
{
    get
    {
        if (_grievances == null)
        {
            _grievances = new PagedCollectionView(Context.lict_grievances);
            _grievances.SortDescriptions.Add(new SortDescription("grievance_type_id", ListSortDirection.Ascending));
        }

        return _grievances;
    }
}

这是我视图中的 DataGrid:

<sdk:DataGrid x:Name="grdGrievances" AutoGenerateColumns="False" ItemsSource="{Binding Path=Grievances}" HorizontalContentAlignment="Center">
    <sdk:DataGrid.Columns>

        <sdk:DataGridTemplateColumn Header="Total # of Outcomes">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Converter={StaticResource GrievanceOutcomeSum}}" Margin="15,0,0,0"></TextBlock>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>

        <sdk:DataGridTemplateColumn Header="Resolved">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=outcome_resolved, Mode=TwoWay}" 
                             TextChanged="ResolvedTextBox_TextChanged" HorizontalAlignment="Center"></TextBox>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>


        <sdk:DataGridTemplateColumn Header="Pending">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=outcome_pending, Mode=TwoWay}"></TextBox>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>

    </sdk:DataGrid.Columns>
</sdk:DataGrid>

这是聚合文本块的值转换器:

public class GrievancesAggregateConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        lict_grievance entity = (lict_grievance)value;
        short i = 0;

        if (entity != null)
        {
            if (entity.outcome_resolved != null)
                i += (short)entity.outcome_resolved;

            if (entity.outcome_pending != null)
                i += (short)entity.outcome_pending;
        }
        return i;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

因此,在更改其他 2 个文本框中的值时,我需要它来刷新文本块中的聚合值。我怎样才能做到这一点?我现在不知所措,浏览网页我找不到任何解决方案。

非常感谢,埃文

4

3 回答 3

1

为什么不将 TextBlock(Total # of Outcomes)绑定到将触发 PropertyChanged 的​​属性。然后从 ResolvedTextBox_TextChanged 事件中设置该值。

尝试这样的事情:

<StackPanel Orientation="Vertical">
    <i:Interaction.Triggers>
        <ei:PropertyChangedTrigger Binding="{Binding FirstTextBox}">
            <ei:ChangePropertyAction TargetObject="{Binding ElementName=textBlock}"
                                     PropertyName="Text"
                                     Value="{Binding Converter={StaticResource TestConverter}}" />
        </ei:PropertyChangedTrigger>
        <ei:PropertyChangedTrigger Binding="{Binding SecondTextBox}">
            <ei:ChangePropertyAction TargetObject="{Binding ElementName=textBlock}"
                                     PropertyName="Text"
                                     Value="{Binding Converter={StaticResource TestConverter}}" />
        </ei:PropertyChangedTrigger>
    </i:Interaction.Triggers>
    <TextBlock x:Name="textBlock" />
    <TextBox x:Name="firstTextBox"
             Text="{Binding FirstTextBox, Mode=TwoWay}" />
    <TextBox x:Name="secondTextBox"
             Text="{Binding SecondTextBox, Mode=TwoWay}" />
</StackPanel>
于 2011-03-11T21:34:25.420 回答
1

Evan, Colin Eberhardt 在他的博客上有一个有趣的所谓的“MultiBinding”解决方案,并且正在做一些非常相似的事情。

“MultiBinding 是一项 WPF 功能,它允许您将单个属性绑定到多个源,源值由值转换器组合。这是 Silverlight 中缺少的功能”(引用)

这应该可以解决您的问题。此致!

于 2011-03-11T22:54:58.283 回答
0

try calling OnApplyTemplate
eg. grid.OnApplyTemplate()

于 2011-12-06T12:41:06.793 回答