Basically, how do I bind (one-way) to a textbox named txtFullName. Initially, any text in the text box gets cleared/blanked out since ToString returns "". But, when I make changes to FirstName or LastName it does not update the binding against FullName. Any way to do this?
Also, is there any way to bind to a method (not just a field)? That is, set the binding directly to the ToString() method and have it update when FirstName or LastName changes?
Oh, and it would be super awesome if there was some sort of generic way of handling this... like an attribute on the FullName field or an attribute on the ToString method telling it what properties to look for for changes.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace AdvancedDataBinding
{
public class UserEntity
{
static UserEntity()
{
FirstNameProperty = DependencyProperty.Register("FirstName", typeof(String), typeof(UserEntity));
LastNameProperty = DependencyProperty.Register("LastName", typeof(String), typeof(UserEntity));
}
public String FirstName
{
get { return (String)GetValue(FirstNameProperty); }
set { SetValue(FirstNameProperty, value); }
}
public static readonly DependencyProperty FirstNameProperty;
public String LastName
{
get { return (String)GetValue(LastNameProperty); }
set { SetValue(LastNameProperty, value); }
}
public static readonly DependencyProperty LastNameProperty;
public String FullName
{
get { return ToString(); }
}
public override string ToString()
{
return FirstName + " " + LastName;
}
}
}