简单的双向绑定在usercontrol

时间:2017-06-17 20:35:39

标签: c# wpf xaml data-binding user-controls

好吧因为某些原因,这个非常简单的例子不起作用,我完全不知道为什么它不起作用..当我输入文本框时,没有任何反应!甚至还在发生什么?!

代码隐藏:

public partial class ucTest : UserControl {
    public ucTest() {
        InitializeComponent();
        (Content as FrameworkElement).DataContext = this;
    }

    public string SomeText {
        get { return (string)GetValue(SomeTextProperty); }
        set { SetValue(SomeTextProperty, value); }
    }

    public static readonly DependencyProperty SomeTextProperty =
        DependencyProperty.Register(
            "SomeText",
            typeof(string),
            typeof(ucTest),
            new FrameworkPropertyMetadata(default(string)));
}

XAML:

<UserControl x:Class="PlainWPFTests.ucTest"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="{Binding SomeText}"/>
    <TextBox Grid.Column="1" Text="{Binding SomeText, Mode=TwoWay}"/>
</Grid>

0 个答案:

没有答案