Silverlight:如何在更改实体中的值时更新 IValueConverter 绑定文本框?

发布于 2024-10-21 16:37:38 字数 3085 浏览 8 评论 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 个文本框中的值时,我需要它来刷新文本块中的聚合值。我怎样才能做到这一点?我现在很茫然,浏览网页找不到任何解决方案。

非常感谢, 埃文

So, I have a DataGrid with a TextBlock that displays the aggregrate value of two textboxes in the grid. I do this by binding using a value converter. This works on the load but I need it to update upon changing the values of the other entities which its aggregating over. Here is some of my code:

Here is the my PagedCollectionView in my ViewModel which is bound to the View.

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;
    }
}

Here is my the DataGrid in my View:

<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>

Here is my Value Converter for the aggregated textblock:

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();
    }
}

So, upon changing the values in the other 2 textboxes I need it to refresh the aggregrated value in the textblock. How can I accomplish this? Im at a loss right now and browsing the web I couldnt find any solutions.

Thanks a bunch,
Evan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吻风 2024-10-28 16:37:38

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

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

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

Evan, Colin Eberhardt has an interesting so called "MultiBinding" solution on his blog and is doing something very similar.

"MultiBinding is a WPF feature that
allows you to bind a single property
to a number of sources, with the
source values being combined by a
value converter. This is a feature
that is missing from Silverlight"
(Quote)

This should solve your problem. Best regards!

一笑百媚生 2024-10-28 16:37:38

为什么不将 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>

Why don't you bind the TextBlock (Total # of Outcomes) to a property that will fire PropertyChanged. Then set that value from the ResolvedTextBox_TextChanged event.

Try something like this:

<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>
—━☆沉默づ 2024-10-28 16:37:38

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

try calling OnApplyTemplate
eg. grid.OnApplyTemplate()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文