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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:42:18+00:00 2026-05-12T08:42:18+00:00

I cannot understand the Silverlight documentation on data binding. I seek a very simple

  • 0

I cannot understand the Silverlight documentation on data binding. I seek a very simple example.

Suppose I define a UserControl in a XAML file. The toplevel child of the UserControl is a Grid, of n columns and m rows. Within the Grid is a Rectangle. Like this:

<UserControl....
    <Grid ...>
        <Rectangle ...>

The rectangle should be displayed in a particular (row, column) within the Grid. This row/column pair changes at runtime, according to various evevnts.

I think I can use data binding for that. Can someone confirm this is a reasonable use of data binding?

I think I can do something like this:

 <Rectangle  x:Name="rect"
     Grid.Column = "{Binding ???}"
     Grid.Row = "{Binding ???}"  />

BUT, I don’t know what to put for the binding.

What goes there? And, How do I validate what I put there? It seems I can put any string I like, even the full text of the Declaration of Independence, and it behaves exactly the same way. Which is to day, it does nothing. It does not work. It does not throw an exception.

I don’t know what that string should be. and I don’t know how to debug what I put there. There seems to be no way to know if it is correct or valid or ridiculous.

A simple example is all it would take. I don’t know why the MSDN doc is so obtuse. Am I thoe only one who cannot understand things like this

In Silverlight, you can bind to the target value of any dependency property. The source property value for a data binding need not be a dependency property; it can be a property on a CLR object if certain conditions are met (see Data Binding). The source object can also be an existing dependency object referenced either by name or by a relative position in the object graph. In Silverlight, you can bind to the target value of any dependency property. The source property value for a data binding need not be a dependency property; it can be a property on a CLR object if certain conditions are met (see Data Binding). The source object can also be an existing dependency object referenced either by name or by a relative position in the object graph.

That kind of documentation makes me want to fall down and cry. Who writes that crap? And the doc page does not include a single example to show what all those words mean. Listen, English is my first language. And I consider myself a .NET developer. But I cannot make any sense what-so-ever of that paragraph. The person or committee who wrote it should be taken out back and shot. Twice.

Even outside te MSDN pages, I haven’t been able to find an example that actually shows how silverlight databbinding works. There are examples in MSDN that show things like

<Binding ...who cares? ....>

But I don’t know what that “” thing is. It’s not in my xaml and I don’t think I want it in my xaml. What I have is an actual control, and a Rectangle within it. Can I specify the Grid.Column or Grid.Row of a rectangle via a databinding?

Now, I told you that the Rectangle lives within a Grid, which is a child of a UserControl.

<UserControl....
    <Grid ...>
        <Rectangle ...>

I get a code-behind file for the UserControl, it looks like this:

public partial class MyThing : UserControl
{
    ....
}

What if I want to bind te position of the rectangle within the grid (within the usercontrol) via an int property on the UserControl. Can I do that? Can I bind the Grid.RowProperty and Grid.ColumnProperty to something ? What if X and Y are like this:

public partial class MyThing : UserControl
{
    public int X { get; set; }
    public int Y { get; set; }
    ....
}

Can I bind that to the rectangle’s grid.row and grid.column? How?

Please, someone clarify and give a simple example.

  • 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-05-12T08:42:18+00:00Added an answer on May 12, 2026 at 8:42 am

    Each FrameworkElement has a DataContext, which (if not null) is an instance of an arbitrary data class. You’re binding to a property of that DataContext.

    <Rectangle Width="{Binding MyRectWidth}" x:Name="_myRect" />
    

    Codebehind:

    public class Foo
    {
        public double MyRectWidth { get; set; }
    }
    
    Foo foo = new Foo();
    foo.MyRectWidth = 100;
    _myRect.DataContext = foo;
    

    The notation {Binding MyRectWidth} is shorthand for {Binding Path=MyRectWidth}.

    You can specify other parameters for the binding, such as an instance of an object that implements IValueConverter, to convert between the source and destination values.

    The programmatic version of the XAML syntax is the Binding object:

    Binding b = new Binding();
    b.Path = "MyRectWidth";
    _myRect.SetBinding(Rectangle.WidthProperty, b);
    

    A FrameworkElement‘s DataContext is in most cases inherited from its parent. You can see how this makes editing a more complex object fairly straightforward.

    <UserControl x:Class="PersonEditControl">
        <StackPanel>
            <TextBox Text="{Binding Name, Mode=TwoWay}" />
            <TextBox Text="{Binding Address, Mode=TwoWay}" />
            <TextBox Text="{Binding Phone, Mode=TwoWay}" />
        </StackPanel>
    </UserControl>
    

    (You’ll notice in this case I’ve set another property on the Binding: Mode=TwoWay. This is necessary to propagate changes in the control back to the DataContext.)

    public class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
    }
    
    public partial class PersonEditControl
    {
        void SetPerson(Person p)
        {
            // child controls inherit this DataContext, if they are
            // not explicitly given another:
            DataContext = p; 
        }
    }
    

    At any point in the control hierarchy, you can specify a new DataContext, and it will be inherited by children from that point downward.

    Silverlight 3 introduces a new binding markup extension, ElementName, which allows you to specify another XAML element as the Binding source. I played around with giving a name to my UserControl (let’s say _myControl) and then specifying {Binding Path=PropertyName, ElementName=_myControl} in a child control’s property, but it didn’t work.

    I didn’t spend much time on it and I might have missed something. At the very least, you can specify this relationship manually:

    public partial class MyThing : UserControl
    {
        public int X { get; set; }
        public int Y { get; set; }
    
        public MyThing()
        {
            InitializeComponent(); // important to call this first: loads the XAML
    
            _myRect.DataContext = this;
        }
    }
    

    Another important piece in this puzzle is the interface INotifyPropertyChanged. If your DataContext implements this interface, and fires the PropertyChanged event when its properties change, Bindings to those properties will pick up on the changes and propagate them to the control.

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

Sidebar

Ask A Question

Stats

  • Questions 190k
  • Answers 190k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You would code it like this: Action<int> an = new… May 12, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer Here is another alternative approach using an ExecutorService. It requires… May 12, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer You could add XmlIgnore to the actual decimal property and… May 12, 2026 at 6:04 pm

Related Questions

I have a project setup - it is a Silverlight Client Application hosted in
I've written a small hello world test app in Silverlight which i want to
I am starting to write an application in Silverlight with RIA services and SilverlightFx.
I cannot understand the Oracle documentation. :-( Does anybody know how to fetch multiple

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.