Reputation: 849
(I'm new to WPF) I have as objects with severals properties which I would like to bind to textbox. I have a textbox control named txtStudentName. looking for some examples got me think I need to use the next method:
$txtStudentName.DataBindings.Add(,,);
But I don't have the DataBindings property in my textbox object.
anyone?
Upvotes: 1
Views: 904
Reputation: 8614
You can also bind in the XAML, if the objects you want to bind to are public and are properties of the object in your data context:
<Window x:Class="CarSystem.AlarmsDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding MyDataContextObject, RelativeSource={RelativeSource Self}}"
Title="LPR - Mobile Plate Hunter 900: Alarms">
<Grid>
<TextBox Text={Binding Path=MyTextProperty, Mode=TwoWay} />
</Grid>
</Window>
Upvotes: 2
Reputation: 2004
Since you tagged WPF, consider doing this in XAML only:
<TextBox Text="{Binding Path=SomeTextProperty, Mode=TwoWay}" />
Upvotes: 0
Reputation: 18578
Bind like this:
TextBox MyText = new TextBox();
Binding binding = new Binding();
binding.Path = new PropertyPath("Name"); //Name of the property in Datacontext
BindingOperations.SetBinding(MyText,TextBox.TextProperty , binding);
If you want to bind to certain object which contains Name property you will have to set binding.Source to that object also.
Upvotes: 0