【问题标题】:Silverlight: How can I update an IValueConverter bound Textbox upon changing values in an Entity?Silverlight:如何在更改实体中的值时更新 IValueConverter 绑定的文本框?
【发布时间】:2011-07-13 19:09:42
【问题描述】:

所以,我有一个带有 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 个文本框中的值时,我需要它来刷新文本块中的聚合值。我怎样才能做到这一点?我现在很茫然,浏览网页我找不到任何解决方案。

非常感谢, 埃文

【问题讨论】:

    标签: silverlight data-binding datagrid ria ivalueconverter


    【解决方案1】:

    Evan, Colin Eberhardt 在他的博客上有一个有趣的所谓的"MultiBinding" solution,并且正在做一些非常相似的事情。

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

    这应该可以解决您的问题。最好的问候!

    【讨论】:

    • 也检查一下,显然科林的解决方案有一些泄漏,我不知道他们是否仍然这样做。 blog.thekieners.com/2010/09/08/…
    • 是的,德里克,谢谢。您还可以检查是否有新的 Silverlight 4 GDR 版本“4.0.60129.0”,它解决了“如果 Silverlight 4 应用程序在控件中有内联数据模板,则会发生内存泄漏”(support.microsoft.com/kb/2495644/en-us
    【解决方案2】:

    为什么不将 TextBlock(结果的总数)绑定到将触发 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>
    

    【讨论】:

    • 因为实体来自 RIA 服务并且是从实体框架生成的实体。所以,我不认为我可以编辑实体对象并将总计属性放在那里。
    • 您能提供一些代码吗?我不知道我会怎么做..
    • 我意识到这不是 DataGrid,如果该示例没有帮助,我也可以尝试。
    • Evan,请不要这样 - 如果您不知道 - 这个不错的解决方案需要“Expression Blend 4 SDK for Silverlight”(或已安装的 Blend)组件。然后您将能够解析“System.Windows.Interactivity”和“Microsoft.Expression.Interactions”命名空间。
    • 感谢您的解决方案,如果我能获得对控件的引用,这将非常有用。我可以为 gridview 中的文本块命名,但如何让行为找到位于同一行的控件?
    【解决方案3】:

    尝试调用 OnApplyTemplate
    例如。 grid.OnApplyTemplate()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 2023-04-04
      • 1970-01-01
      • 2012-04-06
      • 2011-11-02
      • 2011-04-27
      相关资源
      最近更新 更多