WPF Binding To Object Not Working

P

Phil Johnson

Hello,

My first attempt to bind a 'detail' form to the properties on an object and
the binding is not working.

I have the following grid:

<Grid x:Name="MainGrid">
<StackPanel FlowDirection="LeftToRight">
<TextBlock Foreground="#FFFFFFFF">First name:</TextBlock>
<TextBox Text="{Binding Path=UserName}"></TextBox>
</StackPanel>
</Grid>

And the code behind on the load for the form runs the following... its
definitely executing and the UserName property of _user is set to "Phillip",
but nothing displays in the text box detailed above!

this.MainGrid.DataContext = _user;

Is there some obvious properties or calls I need to set or make to get a
simple binding app like this to work? Or am I missing something else?

Thanks.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
 
N

not_a_commie

I'm no expert on this, but I think you need to specify the object that
has the UserName property in your Binding regardless of the
DataContext. Also, is UserName a DependencyProperty? You should
probably invest in some WPF books to get you started.
 
P

Phil Johnson

Thanks for the response...

I've read a bit about it in Essential Windows Presentation Foundation by
Chris Anderson and I'm also looking at this tutorial....

http://coredotnet.blogspot.com/2006/05/wpf-data-binding-tutorial.html

The only thing different in my code is my object does not currently
implement the INotifyPropertyChanged interface, but in Chris Anderson's book
he says that's optional.

Might try implementing the interface and seeing if it binds... not too
hopeful though. If anybody has any ideas I would be grateful.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
 
P

Phil Johnson

Got this one resolved....

My object I was binding to had internal properties, constructors etc. Even
though it is in the same assembly as the form I am binding to it, the binding
did not like the internal access.

I changed the internal to public on the constructor and properties and the
binding works!

I did not get any messages about security or permission or anything. I try
to develop with least priviledges/permissions and try to avoid public
wherever possible... might have to rethink that approach now.

--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top