【问题标题】:TextBlock with databinding calculated value is not updating具有数据绑定计算值的 TextBlock 未更新
【发布时间】:2012-06-08 21:53:55
【问题描述】:

我有一个文本块,它绑定到我的 JobItem 类中名为“AdminTime”的双重属性。 AdminTime 计算在名为 TimeLog 的 observablecollection 中的总管理时间。我使用文本框将这些管理时间添加到 JobItem 类中。这是按比例缩小的代码:

<TextBlock Style="{StaticResource TextBlockStyle}" Grid.Row="1" Grid.Column="1" Name="adminHrs" Text="{Binding Path=AdminTime, UpdateSourceTrigger=Explicit, Mode=OneWay, StringFormat={}{0:0.00}}" />
<TextBox Style="{StaticResource textBoundStyle}" Name="adminTimeTxtBox" Grid.Row="3" Grid.Column="1"  />
<Button Style="{StaticResource addTimeStyle}" Name="addAdBtn" Grid.Column="1" Grid.Row="3" Click="addAdBtn_Click" />
<Button Style="{StaticResource subTimeStyle}" Name="subAdBtn" Grid.Column="1" Grid.Row="3" Click="subAdBtn_Click" />

对于后面的代码,我有 addTime 按钮单击处理程序。请注意,我知道我的 UpdateSourceTrigger 已经是显式的,我不应该更新我的源代码,但我只是在检查它是否能解决我的问题:

public static readonly DependencyProperty AdminTimeProperty =
        DependencyProperty.Register("AdminTime", typeof(double),
        typeof(UpdateJobDialog));
private void addAdBtn_Click(object sender, RoutedEventArgs e)
    {
        AddHours();
    }

    private void AddHours()
    {
        item.AddTime(emp.UserType, emp.UserId, DateTime.Now, double.Parse(adminTimeTxtBox.Text));
        BindingExpression adHr = adminHrs.GetBindingExpression(TextBlock.TextProperty);
        adHr.UpdateSource();
    }

在我的 JobItem 类中,我有以下代码:

public double AdminTime
    {
        get
        {
            double newTime = 0.00;
            if (TimeLog != null)
            {
                foreach (HoursWorked hw in TimeLog)
                {
                    if (hw.EmployeeType == "Admin")
                        newTime += hw.Hours;
                }
            }
            adminTime = newTime;
            return adminTime;
        }

        set 
        {
            if (AdminTime != value)
            {
                adminTime = value;
                OnPropertyChanged("AdminTime");
            }
        }
    }

public void AddTime(string employeeType, string userId, DateTime datePosted, double hours)
    {
        HoursWorked newLog;
        newLog = new HoursWorked(employeeType, userId, datePosted, hours);
        TimeLog.Add(newLog);

    }

public ObservableCollection<HoursWorked> TimeLog
    {
        get { return timeLog; }
        set
        {
            if (!TimeLog.Equals(value))
            {
                timeLog = value;
                OnPropertyChanged("TimeLog");
            }
        }

    }

当我退出窗口然后重新打开它时,我的文本块确实绑定了,我想要的是在我单击 addAdBtn 后更新我的文本块。我不想打开对话框才能看到我的管理时间文本块和按钮,添加时间,关闭对话框,然后再次重新打开以查看更新的文本块。

【问题讨论】:

    标签: wpf data-binding observablecollection textblock updatesourcetrigger


    【解决方案1】:

    我真的想通了,我只需要将我的 updatesourcetrigger 更改为 twoway。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      相关资源
      最近更新 更多