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

  • Home
  • SEARCH
  • 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 8718999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:40:59+00:00 2026-06-13T06:40:59+00:00

I have the following WPF code. You can see in the comments there that

  • 0

I have the following WPF code. You can see in the comments there that I have a problem with my OnValueChanged handler. I need the code there to differentiate between a Value set from the UI (through various bindings) and one set from the manager class. I had hoped that DependencyPropertyChangedEventArgs would have some kind of source that I could use to differentiate this, but I don’t see anything like that. Ideas? Is there some way to set a WPF DependencyProperty without triggering its PropertyChanged handler? Thanks for your time.

public class GaugeBaseControl : UserControl
{
    protected readonly AssetModelManager Manager;
    public GaugeBaseControl(AssetModelManager mgr)
    {
        Manager = mgr;
        if(mgr != null)
            mgr.TelemetryValueChanged += MgrOnTelemetryValueChanged; // coming on background thread
    }

    private void MgrOnTelemetryValueChanged(KeyValuePair<string, object> keyValuePair)
    {
        if(_localTelemetryId != keyValuePair.Key)
            return;

        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
        {
            if (!Equals(Value, keyValuePair.Value))
                Value = keyValuePair.Value;
        }));
    }

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var gbc = (GaugeBaseControl) d;
        var id = gbc.TelemetryId;
        if (!string.IsNullOrEmpty(id))
        {
            // this is the problem:
            // I need to always set gbc.Manager[id] if this event was triggered from the UI (even when equal)
            // however, if it was triggered by TelemetryValueChanged then we don't want to go around in circles
            if (!Equals(gbc.Manager[id], e.NewValue))
                gbc.Manager[id] = e.NewValue;
        }
    }

    private string _localTelemetryId; // to save us a cross-thread check
    private static void OnTelemetryIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var gbc = (GaugeBaseControl)d;
        var tid = gbc.TelemetryId;
        gbc._localTelemetryId = tid;
        gbc.Value = string.IsNullOrEmpty(tid) ? null : gbc.Manager[tid];
    }

    public static readonly DependencyProperty TelmetryIdProperty = DependencyProperty.Register("TelemetryId", typeof(string), typeof(GaugeBaseControl), new PropertyMetadata(OnTelemetryIdChanged));
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), typeof(GaugeBaseControl), new PropertyMetadata(OnValueChanged));

    public object Value
    {
        get { return GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value);}
    }

    public string TelemetryId
    {
        get { return (string)GetValue(TelmetryIdProperty); }
        set { SetValue(TelmetryIdProperty, value); }
    }
}
  • 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-13T06:41:00+00:00Added an answer on June 13, 2026 at 6:41 am

    It seems a bit hackish, but it is the best shot i could come up with without changing the architecture. You could stop listening on the TelemetryValueChanged event while doing your internal update to stop the roundtrip like so:

    internal void SetManagerIdInternal(string id, object value)
    {
        if(mgr != null)
        {
            mgr.TelemetryValueChanged -= MgrOnTelemetryValueChanged;
            mgr[id] = value;
            mgr.TelemetryValueChanged += MgrOnTelemetryValueChanged;
        }
    }
    

    And use it like this:

    if (!Equals(gbc.Manager[id], e.NewValue))
        SetManagerIdInternal(id, e.NewValue);
    

    You could also use a private field to just skip doing work without unregistering/reregistering the event in MgrOnTelemetryValueChanged wich might be better performance wise, but i haven’t tested it.

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

Sidebar

Related Questions

I have the following code that adds a background worker into a VB.net WPF
I am NEW to WPF. I have the following XAML code: </Window> ... <Canvas>
I am trying my hands on WPF MVVM. I have written following code in
I have recently started programming in WPF and bumped into the following problem. I
I have the following WPF code: public Window1() { InitializeComponent(); FlowDocument fd = new
I have the following WPF code and it gives me an exception at TextBox
I have failed to find a Windows Runtime equivalent to the following WPF code
Let's say I have the following code in WPF application: try { // NorthwindDataContext
I have a WPF application in C# that may need to be localized in
I have the following piece of code, which is part of a WPF 4

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.