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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:10:14+00:00 2026-06-09T21:10:14+00:00

I have a question about data binding in WP7 using c# code. If I

  • 0

I have a question about data binding in WP7 using c# code.

If I have custom class, everything is clear. I’m set Source property to my class instance and Path property to a property inside that class. Like this (and it works)

Binding binding = new Binding()
{
  Source = myclass,
  Path = new PropertyPath("myproperty"),
  Mode = BindingMode.TwoWay
};
myButton.SetBinding(dp, binding);

Now, how can I bind simple structures such as Boolean variable or single item from List<>?
If I’ll write Source = myBoolean or Source = List[5] what should I write into Path property? (Note that I need TwoWay binding, thus setting a Path property is mandatory)

Final solution:

To bind a variable, this variable should be a public property and INotifyPropertyChanged should be implemented. List can be replaced for ObservableCollection for this purpose.

All the rest should looks like in nmaait’s answer and entire code should be like this:

public partial class Main : PhoneApplicationPage, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Boolean _myBoolean { get; set; }
    public Boolean myBoolean
    {
        get { return _myBoolean; }
        set { _myBoolean = value; OnPropertyChanged("myBoolean"); }
    }
    ObservableCollection<Int32> myList { get; set; }

    public Main()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(Main_Loaded);
    }

    protected void OnPropertyChanged(String name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    void Main_Loaded(object sender, RoutedEventArgs e)
    {
        myBoolean = false;
        myList = new ObservableCollection<int>() { 100, 150, 200, 250 };


        Binding binding1 = new Binding()
        {
            Source = this,
            Path = new PropertyPath("myBoolean"),
            Mode = BindingMode.TwoWay
        };
        myButton.SetBinding(Button.IsEnabledProperty, binding1);


        Binding binding2 = new Binding()
        {
            Source = myList,
            Path = new PropertyPath("[1]"),
            Mode = BindingMode.TwoWay
        };

        myButton.SetBinding(Button.WidthProperty, binding2);
    }

    private void changeButton_Click(object sender, RoutedEventArgs e)
    {
        myList[1] +=50;
        myBoolean = !myBoolean;
    }
}
  • 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-09T21:10:15+00:00Added an answer on June 9, 2026 at 9:10 pm

    Your source is the data context, so you wouldn’t really be setting the source as the boolean itself but setting the Source as the Class/Element that the boolean belongs to like you have already done. The Path is going to be myBoolean or List[5].

    If the boolean is in the current class you can do

    Binding binding = new Binding()
    {
      Source = this,
      Path = new PropertyPath("myBoolean"),
      Mode = BindingMode.TwoWay
    };
    myButton.SetBinding(dp, binding);
    

    What exactly are you trying to achieve by binding to a list item. If your list changes then you wouldn’t want to bind to a specific index, you can bind to the selected item like this though. Give some more info on what you need to achieve by binding to a specific list item.

    Binding binding = new Binding()
    {
      Source = List,
      Path = new PropertyPath("SelectedItem"),
      Mode = BindingMode.TwoWay
    };
    myButton.SetBinding(dp, binding);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about data binding which I am struggling with. I have
I have a question about ASP.NET MVC3 model binding. If I have a class
I have an question about the following code (Is this call dynamic binding?). I
I have question about interpreting strings as packed binary data in C++. In python,
i have question about YAJLiOS parser... I have next json data : {{ body
I have a question about the use of data structures such as ArrayLists in
I have a question about grabbing a certain value from the html response data
I have few questions about using lock to protect my shared data structure. I
Again a question about compression on wcf services. I'm using netnamedpipe binding to send
My question is about properly binding the property of one element to another. Here

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.