tags:

views:

28

answers:

2

Case:

public class customer
{
    public string Adress { get; set; }
}

Xaml:

<Grid x:Name="LayoutRoot" Background="White" >
  <StackPanel>
      <TextBox Text="{Binding Adress}"/>
  </StackPanel>
</Grid>

.cs

    public MainPage()
    {
        InitializeComponent();
        LayoutRoot.DataContext = new customer() { Adress = "Some Adr" };
    }

So the Question is... In code behind .. How to i get the property (string) of the bind (Adress). I need it to access the customer.adress as a property, to assign another variable. (in this case when a event occours. e.g. after the this.Loaded occours.)

So i got the UI element (sender) and i can get the customer form it's datacontext.

In short. How do i get the property name of the binding object. (the binding object is easy to find i just use datacontext to get the customer, but where can i get the name of the property? that are in the xaml (eg. name) from the sender)

(I plan to use reflection if it is nessesery to access the "adress" inside customer) but how to get the "name" of the property that text in the textBox is bound to.

Regards Jesper

A: 

Try the following:

Give your TextBox a name, so you can access it from code:

<Grid>
    <TextBox Name="textBox" Text="{Binding Adress}" />
</Grid>

In code behind:

BindingExpression bExpr = textBox.GetBindingExpression(TextBox.TextProperty);

The name of the bound property can now be extracted as a string from:

bExpr.ParentBinding.Path.Path
naacal
Thankx for the answer, i can now use reflection to get the property.
ReLoad
A: 

However, I would advise against this; try to use established MVVM principles and find out the bound values in the view-models instead of from the views. If you state your problem a bit more clearly, like for instance, if there are multiple customers in a list and you need the address of the selected one, we might be able to help further.

Alex Paven
Both, there is both multible and single. But it's ok with the first answer. I used it as a base for the solution. It's working perfectly. Maby I can post the solution somewere?
ReLoad