开发者

How to bind to a Textbox

my XAML is

<TextBox Name="DutchName" HorizontalAlignment="Ri开发者_如何学Pythonght" Text="{Binding customer,Path=DutchName }" />

my class is

class customer
{
    Name name;
}

class Name
{
    string DutchName;
    string EnglishName;
}

The TextBox is not bound. Can anyone correct this error please?

Thanks,


i dont think your code would compile for starters,

should be

public class customer
{
    public Name name { get; set; }
}

public class Name
{
    public string DutchName { get; set; }
    public string EnglishName { get; set; }
}

this will enable you to get once and set the properties from xaml, however if you set the properties in code you need to implement INotifyPropertyChanged (otherwise your user interface wont know). From your question i think you need to do a little more study. find out about these topics. (to start with)

  • Properties
  • Accessors (public, private, protected, internal) - you cant bind to a non public property
  • INotifyPropertyChanged

your xaml binding should look like this

<TextBox  HorizontalAlignment="Right" Text="{Binding Path=name.DutchName }" />

if you set the data context of the window/user control you are working in to be the customer. e.g.

....
InitializeComponent();

customer cust = new customer();
cust.Name = new Name { DutchName = "Sigfried", EnglishName = "Roy" };
this.DataContext = cust;
....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜