Question

I have a textblock that is binding to a double property called "AdminTime" in my JobItem class. AdminTime calculates the total admin hours that are in an observablecollection called TimeLog. I use a textbox to add these admin hours into the JobItem class. Here is the scaled down code:

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

And for the code behind I have the addTime button click handler. Please note that I understand that my UpdateSourceTrigger is already on explicit and that I should not update my source, but I was just checking if it would help my problem:

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

and in my JobItem Class, I have this code in place:

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

    }

My textblock does bind when I exit the window and then reopen it, what I want is to have my textblock update once I click on the addAdBtn. I don't want to have to open the dialog to be able to see my admin time textblock and button, add the time, close the dialog, and reopen again to see an updated textblock.

Was it helpful?

Solution

I actually figured it out, I just needed to change my updatesourcetrigger to twoway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top