Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文本框绑定值int,以便使用它_C#_Wpf_Xaml - Fatal编程技术网

C# 文本框绑定值int,以便使用它

C# 文本框绑定值int,以便使用它,c#,wpf,xaml,C#,Wpf,Xaml,如何获取文本框的值并尝试将其作为int与绑定一起使用 <TextBox Text="{Binding SelectedAmount}"/> 这是我的主要类,但是文本框的值保持为0,它不会改变 公共部分类主窗口:窗口 { }您可以毫不费力地将文本绑定到int 使用绑定时,应该从接口INotifyPropertyChanged或类DependencyObject派生包含可绑定属性的类。否则,绑定将仅显示默认(初始)值 或 或 只需像这样使用 public void BtnC

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

<TextBox Text="{Binding SelectedAmount}"/>
这是我的主要类,但是文本框的值保持为0,它不会改变

公共部分类主窗口:窗口 {


}

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

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


只需像这样使用

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

你是如何定义你的财产的?请包括
SelectedAmount
public string SelectedAmount{get{return{return}SelectedAmount;}set{{SelectedAmount=value;}}}private string{u selectedWeeks;所以绑定不起作用,因为属性不支持绑定。我已经尝试了你的解决方案,但是它不起作用,那么我建议先读一些基础知识。你忘记了这是不必要的,因为输入控件(如TextBox)的默认绑定模式是双向的。不需要启动“\u selectedAmount”,int的默认值为0:)。我不认为在道具的设定器中检查backingfield值有什么意义。。。如果没有更改,属性不会通知(AFAIK)。不确定通过XAML设置UserControl的Datacontext是否是个好主意。。。如何访问实例数据?我也不会将道具定义为ViewModel中的依赖属性,您需要为VM中的每个道具添加一个额外的处理程序来捕获更改并运行逻辑。但这可能更多的是口味问题。。。。没有围墙,我很高兴了解新的方面,如果他们成立。:)@dba有很多优点
0
是多余的,所以我删除了它-在MSDN示例中建议进行值检查(如果您看到代码下面的链接)-IMO定义vm的位置并不重要,只要您能够使其工作(品味问题)-使用DP的AFAIK绑定比INotifyPropChange更高效、更方便(经常需要中断xaml来运行一行CS代码)@dba这个额外的处理程序实际上可以很好地处理xaml,因为它们都使用相同的基础结构。我曾经读到,如果VM不能成为deObject(如poco),就应该使用INotifyPropChange但是与xaml如此相似的DP使可执行文件更具响应性。尽管我不完全确定这到底意味着什么!@EyadKelleh,您是否从ff.SelectedAmount中获得了任何值?请查看您的输出窗口以了解绑定错误。
    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;
    }
}
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));
}
//in view's constructor:
this.DataContext = new MyClass();
<UserControl>
    <UserControl.DataContext>
        <vm:MyClass/>
    </UserControl.DataContext>
</UserControl>
    public void BtnCompute_Click_1(object sender, RoutedEventArgs e)
    {
        MyClass ff = new MyClass();
        int amount;
        int.TryParse(ff.SelectedAmount, out amount);
        deposit = amount;
    }