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 9221291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:32:49+00:00 2026-06-18T03:32:49+00:00

As an exercise I decided to create a bicycle gear calculator in WPF. I

  • 0

As an exercise I decided to create a bicycle gear calculator in WPF. I created two private fields with setters that trigger OnPropertyChanged(), but I have one databound property, ratio, which behaves as “readonly”, because it is dynamically calculated. When I run the program, the textboxes show, the initial values are correctly displayed, the “working” word in the property changed handler is displayed, but the ratio TextBlock doesn’t update.

I suspect this is due to the way the property is “getted”, I wonder if it would be absolutely necessary to add a private field for every, I wonder if there should be any DependencyProperty in this… But actually I have reached my knowledge limit on this and cannot get this trivial program to work.


This is my model:

class SingleGearsetModel : INotifyPropertyChanged
{
    public SingleGearsetModel()
    {
        crank = 44;
        cog = 16;
    }

    private int _crank;
    private int _cog;

    public int crank { 
        get{return _crank;}
        set{
            _crank = value;
            OnPropertyChanged("crank");
        }
    }

    public int cog {
        get{return _cog;}
        set{
            _cog = value;
            OnPropertyChanged("cog");
        } 
    }

    public double ratio
    {
        get {
            return (double)crank / (double)cog;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string arg)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(arg));
            Console.Writeline("working");
        }
    }

} // end class

This is my XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="CalculadorFixaWPF.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">

    <DockPanel x:Name="LayoutRoot">
            <TextBox Text="{Binding crank, Mode=TwoWay}"/>
            <TextBox Text="{Binding cog, Mode=TwoWay}"/>
            <TextBlock Text="{Binding ratio, StringFormat={}{0:0.00}}"/>
    </DockPanel>
</Window>

And this is in my code-behind (MainWindow.xaml.cs):

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        this.DataContext = new SingleGearsetModel();
    }
}

Thanks for reading!

  • 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-18T03:32:51+00:00Added an answer on June 18, 2026 at 3:32 am

    Since Ratio is a calculated field, you want to add a notification every time the value might change. This will happen if crank or cog are changed.

    So, after you notify on them being changed, also notify thatratio has changed:

    public int crank 
    { 
        get{return _crank;}
        set{
            _crank = value;
            OnPropertyChanged("crank");
    
            // *** Notify that ratio has also been changed ***
            OnPropertyChanged("ratio");
        }
    }
    

    Same goes for cog.

    Edit:
    Based on your comments, here is how you can register to the PropertyChanged event from inside your class and raise a PropertyChanged for ratio:

    // Constructor
    public SingleGearsetModel()
    {
        ....
       PropertyChanged += SingleGearsetModel_PropertyChanged;
    }
    
    // PropertyChanged event handler
    void SingleGearsetModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
            if (e.PropertyName == "cog" || e.PropertyName == "crank")
            {
                OnPropertyChanged("ratio");
            }
    }
    

    Using this approach, you don’t need to add the OnPropertyChanged("ratio") inside the setters of the properties – whenever they are raised, you get the event(in SingleGearsetModel_PropertyChanged) and raise the OnPropertyChanged("ratio")

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

Sidebar

Related Questions

The exercise says: Create a Text class that contains a string object to hold
As an exercise with accessibility and a personal challenge to myself I decided that
Hey, This is just a simple exercise from class that I decided to have
Hello for a school exercise i need to create a game and i decided
I decided to port the class in C# below to F# as an exercise.
As a learning exercise i want to create a simple web api and consume
As an exercise in both writing Windows Services and communicating with them, I've decided
As of last night, I decided to start learning about WPF and have been
I'm doing a book exercise that says to write a program that generates psuedorandom
In Introduction to algorithms, 3rd edition exercise 24.3-5 wants an example that this is

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.