UserControl中的TextBox,无法从后面的代码编辑Text依赖属性

时间:2012-07-11 07:55:37

标签: c# wpf xaml data-binding dependency-properties

我的UserControl包含一个TextBox和一个Button。 TextBox的Text由名为X的依赖项属性正确填充。

我的目标: 当我按下按钮时,更改X的值(例如TextBox的文本)。

我已经按如下方式定义了UserControl:

<StackPanel Orientation="Horizontal" >
    <TextBox Name="Xbox" Text="{Binding Path=X}" Width="50"/>
    <Button Content="Current" Click="InsertCurrentBtnClick" />
</StackPanel>

使用codebehind:

    public double X
    {
        get { return (double)GetValue(XProperty); }
        set { SetValue(XProperty, value); }
    }

    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register("X", typeof(double), typeof(MyUserControl), new PropertyMetadata(0.0));


    private void InsertCurrentBtnClick(object sender, RoutedEventArgs e)
    {
        X = 0.7;

        //BindingOperations.GetBindingExpression(this, XProperty).UpdateTarget();
        //BindingOperations.GetBindingExpression(Xbox, TextBox.TextProperty).UpdateTarget();
        //BindingOperations.GetBindingExpression(Xbox, XProperty).UpdateTarget();
        //Xbox.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
        //GetBindingExpression(XProperty).UpdateTarget();
    }

我尝试了几件事 - 一次一件 - (见下文X=0.7;)强制更新TextBox文本,但到目前为止没有任何帮助。

提前致谢。

3 个答案:

答案 0 :(得分:2)

我会这样写:

    public double X
    {
        get { return (double)GetValue(XProperty); }
        set { SetValue(XProperty, value); }
    }

    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register("X", typeof(double), typeof(MainPage), new PropertyMetadata(new PropertyChangedCallback(Callback)));


    public static void Callback(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        (o as MainPage).Xbox.Text = e.NewValue.ToString();
    }

    private void InsertCurrentBtnClick(object sender, RoutedEventArgs e)
    {
        X = 0.7;
    }

和xaml代码:

    <StackPanel Orientation="Horizontal" >
        <TextBox Name="Xbox" Width="50"/>
        <Button Content="Current" Click="InsertCurrentBtnClick" />
    </StackPanel>

答案 1 :(得分:1)

您需要为您控制DataContext。正如我在控件中定义X所示,您需要执行此操作:

    public MyUserControl()
    {
        InitializeComponent();

        // add this line
        this.DataContext = this;
    }

答案 2 :(得分:1)

虽然,你也可以绑定它,只需更改xaml:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Name="myWidnow"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel Orientation="Horizontal" >
        <TextBox Name="Xbox" Width="50" Text="{Binding ElementName=myWidnow, Path=X}" />
        <Button Content="Current" Click="InsertCurrentBtnClick" />
    </StackPanel>
</Grid>

请注意,我已将Name proeprty添加到UserControl。 在这种情况下,您不必更改代码behid中的任何内容。