TextBox绑定值int,以便使用它

时间:2018-07-22 10:42:52

标签: c# wpf xaml

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

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

我已经尝试过了,但是绑定的值是0

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

那是我的主班,但是文本框的valau保持为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;
    }
}

}

2 个答案:

答案 0 :(得分:1)

您可以毫不费力地将Text绑定到int

使用绑定时,应该从接口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();
            }
        }
    }
}

as in here

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>

答案 1 :(得分:0)

像这样简单地使用

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