首页 > 解决方案 > TextBox 绑定值 int,为了使用它

问题描述

如何获取文本框的值并尝试将其用作具有绑定的 int?

<TextBox Text="{Binding SelectedAmount}"/>

我试过这样,但绑定的值为0

public string SelectedAmount 
{ 
    get { return _selectedAmount; } 
    set { _selectedAmount = value; } 
}

那是我的主要课程,但文本框的值保持为 0,它不会改变

公共部分类 MainWindow : Window {

    int deposit;
    int weeks;
    int total;


    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MyClass();

    }

    public class MyClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public int _selectedAmount;
        public int SelectedAmount
        {
            get
            {
                return this._selectedAmount;
            }

            set
            {
                if (value != this._selectedAmount)
                {
                    this._selectedAmount = value;
                    NotifyPropertyChanged();
                }
            }
        }
    }

    public void BtnCompute_Click_1(object sender, RoutedEventArgs e)
    {
        MyClass ff = new MyClass();
        int cc = ff.SelectedAmount;
        deposit = cc;
    }
}

}

标签: c#wpfxaml

解决方案


您可以毫不费力地绑定Textint

使用绑定时,您应该从接口INotifyPropertyChanged或类派生包含可绑定属性的类DependencyObject。否则绑定将仅显示默认(初始)值。

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property.
    // The CallerMemberName attribute that is applied to the optional propertyName
    // parameter causes the property name of the caller to be substituted as an argument.
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public int _selectedAmount;
    public int SelectedAmount 
    {
        get
        {
            return this._selectedAmount;
        }

        set
        {
            if (value != this._selectedAmount)
            {
                this._selectedAmount = value;
                NotifyPropertyChanged();
            }
        }
    }
}

就像在这里

或者

public class MyClass : DependencyObject
{
/// <summary>
/// Gets or Sets SelectedAmount Dependency Property
/// </summary>
public int SelectedAmount 
{
    get { return (int)GetValue(SelectedAmountProperty); }
    set { SetValue(SelectedAmount Property, value); }
}
public static readonly DependencyProperty SelectedAmountProperty =
    DependencyProperty.Register("SelectedAmount ", typeof(int), typeof(MyClass), new PropertyMetadata(0));
}

也不要忘记设置DataContext您的视图。

//in view's constructor:
this.DataContext = new MyClass();

或者

<UserControl>
    <UserControl.DataContext>
        <vm:MyClass/>
    </UserControl.DataContext>
</UserControl>

推荐阅读