c# - 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;
}
}
}
解决方案
您可以毫不费力地绑定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();
}
}
}
}
或者
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>
推荐阅读
- spring-boot - Thymeleaf th:field 不绑定输入文本的值
- python - Pandas 中的时间序列数据集到每小时特征数据集
- python - 使用 ListView 上的多个数据过滤“get_context_data”
- database - 高效的数据库搜索算法
- angular - 剑道 UI 树视图与复选框一起被锁定
- python-3.x - 发生异常:ImportError cannot import name '_typeconv' while using librosa
- c# - 检查 String 中的值是否与数组 C# unity中的元素匹配,
- c# - 如何在 C# 中将多个数组连接为单个数组?
- http - Angular 5 在 Http Post 方法中隐藏凭据的最佳方法
- android - 使用 Picasso(或 Glide)和改造在 RecyclerView 中显示图像,仅适用于经过身份验证的用户