(私はWPFが初めてです)テキストボックスにバインドしたいいくつかのプロパティを持つオブジェクトとして持っています。txtStudentName という名前のテキスト ボックス コントロールがあります。いくつかの例を探していると、次の方法を使用する必要があると思いました。
$txtStudentName.DataBindings.Add(,,);
しかし、テキスト ボックス オブジェクトに DataBindings プロパティがありません。
誰でも?
バインドするオブジェクトがパブリックであり、データ コンテキスト内のオブジェクトのプロパティである場合は、XAML でバインドすることもできます。
<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>
WPF にタグを付けたので、これを XAML でのみ行うことを検討してください。
<TextBox Text="{Binding Path=SomeTextProperty, Mode=TwoWay}" />
次のようにバインドします。
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);
Name プロパティを含む特定のオブジェクトにバインドする場合は、そのオブジェクトにも binding.Source を設定する必要があります。