Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:39:57+00:00 2026-06-08T22:39:57+00:00

I have an object that is created, and want to bind to a property

  • 0

I have an object that is created, and want to bind to a property of that object through the mode OneWayToSource explicitly. This binding however is not working at all. It also has a red border around the textbox right when the program is initialized, when I only want the input validated once I click the button. My last ditch effor was embedding the source to the element itself, but no such luck. Here is what I have:

<StackPanel.Resources>
    <my:HoursWorked x:Key="hwViewSource" /> 
</StackPanel.Resources>

<TextBox Style="{StaticResource textBoundStyle}" Name="adminTimeTxtBox">
    <Binding Source="{StaticResource hwViewSource}" Path="Hours" UpdateSourceTrigger="PropertyChanged" Mode="OneWayToSource">
        <Binding.ValidationRules>
            <my:NumberValidationRule ErrorMessage="Please enter a number in hours." />
        </Binding.ValidationRules>
    </Binding>
</TextBox>

The HoursWorked object looks like this:

//I have omitted a lot of things so it's more readable
public class HoursWorked : INotifyPropertyChanged
{

    private double hours;

    public HoursWorked()
    {
        hours = 0;
    }

    public double Hours
    {
        get { return hours; }
        set 
        {
            if (Hours != value)
            {
                hours = value;
                OnPropertyChanged("Hours");
            }
        }
    }

    #region Databinding
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
    #endregion
}

once the window is initialized, this is the portion of code I have:

public partial class Blah : Window
{
     private HoursWorked newLog;

public Blah()
{
    InitializeComponent();
    newLog = new HoursWorked();
    adminTimeTxtBox.DataContext = newLog;
}

private void addAdBtn_Click(object sender, RoutedEventArgs e)
{
    AddHours();

}

private void AddHours()
{
    if (emp.UserType.Name == "Admin")
    {
        if(!ValidateElement.HasError(adminTimeTxtBox))
        {
                item.TimeLog.Add(newLog);
                UpdateTotals();
                adminTimeTxtBox.Clear();
        }
         }

    }
}

and finally ValidateElement looks like this:

public static class ValidateElement
{
    public static bool HasError(DependencyObject node)
    {
        bool result = false;
        if (node is TextBox)
        {
            TextBox item = node as TextBox;
            BindingExpression be = item.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();
        }
        if (Validation.GetHasError(node))
        {
            // If the dependency object is invalid, and it can receive the focus,
            // set the focus
            if (node is IInputElement) Keyboard.Focus((IInputElement)node);
            result = true;
        }

        return result;

    }
}

It validates properly, but every time I check to see if the property updates, it doesn’t. I really need help on this, any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:39:59+00:00Added an answer on June 8, 2026 at 10:39 pm

    You’ve got 2 instances of the HoursWorked class.

    One is created in Resources via this tag <my:HoursWorked x:Key="hwViewSource" /> but then you create one in your Window with newLog = new HoursWorked(); and set it into the DataContext of the adminTimeTxtBox…so the one your Binding to (the Resource one) isn’t the same as the one you’re updating (the one inside Window).

    You could change the Binding to

    <Binding Source="{Binding}" ….

    and then don’t need the one defined in Resource.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have created an IE band object (toolbar) that is working well. however, when
I have a object that is created through deserialization of some XML. I used
I want to create a Javascript class/object that allow me to have various method:
I have an object that hasn't been created yet (so it has no ID),
I have an ExpenseType object that I have created with the following migration: class
I have a list of class object that I created as a variable in
Good day, I have created an object that will manage data access. My app
I've created a object that I'd like to have accessible from wherever the request-object
I have a stdClass object created from json_decode that won't return the right number
I have created a method that should ideally take a single Account object and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.