0

我在 XAML 中完成了以下代码。但是,我不想在 XAML 中创建文本绑定。反正有没有以编程方式在 C# 中创建相同的方法?

XAML 代码:

<TextBox Name="contentBox"  Text="{Binding Content, Mode=TwoWay}" AcceptsReturn="True" />
4

3 回答 3

1
TextBox tb = new TextBox();
tb.Name = "contentBox";
tb.AcceptsReturn = true;
Binding b = new Binding("Content");
b.Mode = BindingMode.TwoWay;
b.Source = this; // set you DataContext here
tb.SetBinding(TextBlock.TextProperty, b);
于 2013-09-19T07:19:02.913 回答
0

它看起来像这样:

        var tb = new TextBox();
        tb.SetValue(NameProperty, "contentBox");
        tb.AcceptsReturn = true;
        var b = new Binding("Content");
        b.Mode = BindingMode.TwoWay;
        b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        BindingOperations.SetBinding(tb, TextBox.TextProperty, b);

您仍然需要INotifyPropertyChanged为您DataContext的 .

于 2013-09-19T08:29:57.257 回答
0
        Binding binding = new Binding();
        binding.Mode = BindingMode.TwoWay;
        binding.Path = new PropertyPath("Content"); //Name of the property in Datacontext
        BindingOperations.SetBinding(contentBox,TextBox.TextProperty , binding);
于 2013-09-19T07:19:09.750 回答