简体   繁体   English

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

[英]TextBox in UserControl, can't edit Text dependency property from code behind

My UserControl contains a TextBox and a Button. 我的UserControl包含一个TextBox和一个Button。 The TextBox's Text is correctly populated by a dependency property called X. TextBox的Text由称为X的依赖项属性正确填充。

My Goal: Change the value of X (eg Text of the TextBox) when I press the Button. 我的目标:当我按下按钮时,更改X的值(例如,文本框的文本)。

I have defined the UserControl as follows: 我已经定义了UserControl如下:

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

With 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();
    }

I have tried several things - one at a time - (see below X=0.7; ) to force the update to the TextBox Text but nothing has helped so far. 我尝试了几件事-一次一件-(请参阅下面的X=0.7; )来强制更新到TextBox Text,但到目前为止没有任何帮助。

Thanks in advance. 提前致谢。

I'd write it in this way: 我会这样写:

    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;
    }

And the xaml code: 和xaml代码:

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

You need to set the DataContext for you Control. 您需要为控件设置DataContext As I see X defined in your control, you need to do this : 正如我在控件中定义的X ,您需要执行以下操作:

    public MyUserControl()
    {
        InitializeComponent();

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

Although, you can bind it as well, just change the xaml: 虽然,您也可以绑定它,但是只需更改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>

Notice that I've added the Name proeprty to the UserControl. 请注意,我已将名称属性添加到UserControl。 In this case, you don't have to change anything in the code behid. 在这种情况下,您无需在隐藏的代码中进行任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM