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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:14:15+00:00 2026-06-05T18:14:15+00:00

I am basically doing a demo so don’t ask why. It’s very easy to

  • 0

I am basically doing a demo so don’t ask why.

It’s very easy to bind it using XAML:

c# code:

public class MyData
    {
        public static string _ColorName= "Red";

        public string ColorName
        {
            get
            {
                _ColorName = "Red";
                return _ColorName;
            }
        }
    }

XAML code:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <c:MyData x:Key="myDataSource"/>
    </Window.Resources>

    <Window.DataContext>
        <Binding Source="{StaticResource myDataSource}"/>
    </Window.DataContext>

    <Grid>
        <Button Background="{Binding Path=ColorName}"
          Width="250" Height="30">I am bound to be RED!</Button>
    </Grid>
</Window>

But I am trying to achieve the same binding use c# setBinding() function:

        void createBinding()
        {
            MyData mydata = new MyData();
            Binding binding = new Binding("Value");
            binding.Source = mydata.ColorName;
            this.button1.setBinding(Button.Background, binding);//PROBLEM HERE             
        }

The problem is in the last line as setBinding’s first parameter is a Dependency Property but Background is not… So I cannot find a suitable Dependency Property in Button class here.

        this.button1.setBinding(Button.Background, binding);//PROBLEM HERE             

But I can easily achieve similar thing for TextBlock as it has a Dependency Property

  myText.SetBinding(TextBlock.TextProperty, myBinding);

Can anyone help me with my demo?

  • 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-05T18:14:17+00:00Added an answer on June 5, 2026 at 6:14 pm

    You have two problems.

    1) The BackgroundProperty is a static field that you can access for the binding.

    2) Additionally, when creating the binding object, the string you pass in is the name of the Property. The binding “source” is the class that contains this property. By using binding(“Value”) and passing it a string property, you were grabbing the value of the string. In this case, you need to grab the Color property (a Brush) of your MyData class.

    Change your code to:

    MyData mydata = new MyData();
    Binding binding = new Binding("Color");
    binding.Source = mydata;
    this.button1.SetBinding(Button.BackgroundProperty, binding);
    

    Add a Brush Property to your MyData Class:

    public class MyData
    {
        public static Brush _Color = Brushes.Red;
        public Brush Color
        {
            get
            {
                return _Color;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm setting up a C++ project, on Ubuntu x64, using Eclipse-CDT. I'm basically doing
I am using an imagelist with a label control, basically doing custom rolleover affect.
I'm trying to find a way of basically doing a late eval , using
I am basically doing a tutorial but my program seems to have errors that
I'm basically just doing some tests to figure out a good way to write
Basically what I'm doing is checking for the existence of an object, if it's
Basically, I'm doing a lot with Visual Studio lately and in my quest to
Basically, I'm doing this: var phone = $(#phone).val(); Then I want to check if:
I basically have a unix process running and it is doing some heavy processing
I am doing some pre-processing on my database. Basically two queries, one is INSERT

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.