1

(私はWPFが初めてです)テキストボックスにバインドしたいいくつかのプロパティを持つオブジェクトとして持っています。txtStudentName という名前のテキスト ボックス コントロールがあります。いくつかの例を探していると、次の方法を使用する必要があると思いました。

$txtStudentName.DataBindings.Add(,,);

しかし、テキスト ボックス オブジェクトに DataBindings プロパティがありません。

誰でも?

4

3 に答える 3

2

バインドするオブジェクトがパブリックであり、データ コンテキスト内のオブジェクトのプロパティである場合は、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>
于 2013-09-06T13:21:50.000 に答える
0

WPF にタグを付けたので、これを XAML でのみ行うことを検討してください。

<TextBox Text="{Binding Path=SomeTextProperty, Mode=TwoWay}" />
于 2013-09-06T13:15:35.060 に答える
0

次のようにバインドします。

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 を設定する必要があります。

于 2013-09-06T13:13:21.757 に答える