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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:04:36+00:00 2026-06-03T01:04:36+00:00

I want to have a singleton that holds some values S1 and S2 that

  • 0

I want to have a singleton that holds some values S1 and S2 that I can bind to. The goal is to have some UIElements update when its value changes. The problem is that I want to use the value inside of a reused DataTemplate. That means that I cannot bind directly to a dependency property of the singleton but this has to be set outside.

To correctly pass updates the values have to be DependencyProperty. Because I dont know to which property I have to bind I created another attachable property AttProperty of the same type as the values. Now I tried to bind the S1 to AttProperty but this gives me an error:

Additional information: A ‘Binding’ cannot be set on the
‘SetAttProperty’ property of type ‘TextBox’. A ‘Binding’ can only be
set on a DependencyProperty of a DependencyObject.

So how can I bind with an attachable DependencyProperty to another DependencyProperty?

Here is the code for the singleton I have so far (C#):

public class DO : DependencyObject
{
  // Singleton pattern (Expose a single shared instance, prevent creating additional instances)
  public static readonly DO Instance = new DO();
  private DO() { }
  public static readonly DependencyProperty S1Property = DependencyProperty.Register(
    "S1", typeof(string), typeof(DO),
    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  public string S1
  {
    get { return (string)GetValue(S1Property); }
    set { SetValue(S1Property, value); }
  }
  public static readonly DependencyProperty AttProperty = DependencyProperty.RegisterAttached( 
    "Att", typeof(string), typeof(DO), 
    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender) );
  public static void SetAttProperty(DependencyObject depObj, string value)
  {
    depObj.SetValue(AttProperty, value);
  }
  public static string GetAttProperty(DependencyObject depObj)
  {
    return (string)depObj.GetValue(AttProperty);
  }
}

Here is the problematic thing (XAML):

<TextBox Name="Input" Text="" TextChanged="Input_TextChanged" local:DO.AttProperty="{Binding Source={x:Static local:DO.Instance}, Path=S1}" />

Update

With the changes of Bojin Li the errors go away. But one issue remains – if I now try to update the singleton with the help of the attached property like this:

<TextBox local:DO.Att="{Binding Source={x:Static local:DO.Instance}, Path=S1, Mode=TwoWay}" Text="{Binding Path=(local:DO.Att), RelativeSource={RelativeSource Self}, Mode=TwoWay}"/>

Why is the value not propagated to S1 in the singleton?

  • 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-03T01:04:38+00:00Added an answer on June 3, 2026 at 1:04 am

    You need to implement INotifyPropertyChanged and wire the changes up to the dependency property changing.

    public class DO : DependencyObject,INotifyPropertyChanged {
            // Singleton pattern (Expose a single shared instance, prevent creating additional instances) 
            public static readonly DO Instance = new DO();
            private DO() { }
            public static readonly DependencyProperty S1Property = DependencyProperty.Register(
              "S1", typeof(string), typeof(DO),
              new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender,onS1Changed));
    
            private static void onS1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) {
                DO item = d as DO;
                if (item != null) item.OnPropertyChanged(new PropertyChangedEventArgs("S1"));
            }
    
            public string S1 {
                get { return (string)GetValue(S1Property); }
                set { SetValue(S1Property, value); }
            }
            public static readonly DependencyProperty AttProperty = DependencyProperty.RegisterAttached(
              "Att", typeof(string), typeof(DO),
              new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender,onAttChanged));
    
            private static void onAttChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
                DO item = d as DO;
                if (item != null) item.OnPropertyChanged(new PropertyChangedEventArgs("Att"));
            }
    
            public static void SetAttProperty(DependencyObject depObj, string value) {
                depObj.SetValue(AttProperty, value);
            }
            public static string GetAttProperty(DependencyObject depObj) {
                return (string)depObj.GetValue(AttProperty);
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void OnPropertyChanged(PropertyChangedEventArgs e) {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, e);
            }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have singleton class that its object is not statically created. having
I have some bindings that are setup as singletons. I want them to always
I have a singleton logger that contains a vector. Objects from outside can append
I want to have dict / list to which I can add values, just
I have war app that uses reasteasy to expose some services. I want to
I want to have a Singleton that will be auto instantiated on program start.
I have a singleton Session that I want instantiated at application launch. How do
I have a singleton class that I want accessible via a helper method on
I have use a singleton class that contained just one passed value. I then
I have a singleton class that is shared by some threads. within a method

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.