Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# WPF多绑定文本块未更新_C#_Binding - Fatal编程技术网

C# WPF多绑定文本块未更新

C# WPF多绑定文本块未更新,c#,binding,C#,Binding,我想创建一个程序,计算重复一个过程一定次数所需的时间。对于这个例子,我已经缩小了很多 因此,我有一些绑定到类中属性的文本框: Count: <TextBox x:Name="txtCount" Text="{Binding Count, Mode=TwoWay}" Width="50"/> Days: <TextBox x:Name="txtDays" Text="{Binding Days, Mode=TwoWay}" Width="50"/> 我可以使用Count属

我想创建一个程序,计算重复一个过程一定次数所需的时间。对于这个例子,我已经缩小了很多

因此,我有一些绑定到类中属性的文本框:

Count: <TextBox x:Name="txtCount" Text="{Binding Count, Mode=TwoWay}" Width="50"/>
Days: <TextBox x:Name="txtDays" Text="{Binding Days, Mode=TwoWay}" Width="50"/>
我可以使用Count属性更新多绑定textblock,但是Days属性始终显示0,即使Days输入准确地反映了更改。我相信这是因为我的访问器在几天内是不同的,即Set方法。此类位于不同的文件中

public class Sample : INotifyPropertyChanged
    {
        private int _count;
        private TimeSpan _span;

        public int Count 
        { 
            get { return _count; } 
            set 
            { 
                _count = value;
                NotifyPropertyChanged("Count"); /* Doesn't seem to be needed, actually */
            } 
        }

        public TimeSpan Span { get { return _span; } }

/* The idea is to provide a property for Days, Hours, Minutes, etc. as conveniences to the inputter */

        public double Days
        {
            get { return _span.Days; }
            set
            {
                TimeSpan ts = new TimeSpan();
                double val = value > 0 ? value : 0;
                ts = TimeSpan.FromDays(val);
                _span.Add(ts); /* !! This turned out to be the problem, lol - see SixLetterVariables' answer below. */
                NotifyPropertyChanged("Span"); /* Here I can only get it to work if I notify that Span has changed - doesn't seem to be aware that the value behind Days has changed. */
            }
        }

        private void NotifyPropertyChanged(string property)
        {
            if (null != this.PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        public Sample()
        {
            _count = 0;
            _span = new TimeSpan();
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

首先,
TimeSpan
是一个不可变的结构,因此您需要存储任何操作的结果,否则它实际上是不可操作的。此外,您还需要调用
OnPropertyChanged
来更改
Span
Days

public double Days
{
    get { return _span.Days; }
    set
    {
        double val = value > 0 ? value : 0;

        // TimeSpan is an immutable struct, must store the result of any
        // operations on it
        _span = TimeSpan.FromDays(val);

        this.OnPropertyChanged("Days");
        this.OnPropertyChanged("Span");
    }
}

// This is preferred way for handling property changes
private event PropertyChangedEventHandler propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
    add { this.propertyChanged += value; }
    remove { this.propertyChanged -= value; }
}

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = this.propertyChanged;
    if (null != handler)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

首先,
TimeSpan
是一个不可变的结构,因此您需要存储任何操作的结果,否则它实际上是不可操作的。此外,您还需要调用
OnPropertyChanged
来更改
Span
Days

public double Days
{
    get { return _span.Days; }
    set
    {
        double val = value > 0 ? value : 0;

        // TimeSpan is an immutable struct, must store the result of any
        // operations on it
        _span = TimeSpan.FromDays(val);

        this.OnPropertyChanged("Days");
        this.OnPropertyChanged("Span");
    }
}

// This is preferred way for handling property changes
private event PropertyChangedEventHandler propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
    add { this.propertyChanged += value; }
    remove { this.propertyChanged -= value; }
}

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = this.propertyChanged;
    if (null != handler)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

我也这么想,但没什么区别。修复代码后,您需要保存
\u span
TimeSpan.Add
不会修改
TimeSpan
,而是返回一个新的时间跨度。您确定了它。我真是个笨蛋。非常感谢:)另外,您可能只需要
\u span=TimeSpan.FromDays(val)
,但我不确定您的意图。由于代码当前处于保存状态,
Days
的所有更新都会将新值添加到旧值中。是的,您是对的。这将有更多的,与种子到周期波动的能力,等等。我只是试图削减它的简洁性,同时给出了一个合理的集合方法表示;可能会更清楚。再次感谢。我也这么想,但没什么区别。修复了代码,您需要保存
\u span
TimeSpan.Add
不会修改
TimeSpan
,而是返回一个新的时间跨度。您确定了它。我真是个笨蛋。非常感谢:)另外,您可能只需要
\u span=TimeSpan.FromDays(val)
,但我不确定您的意图。由于代码当前处于保存状态,
Days
的所有更新都会将新值添加到旧值中。是的,您是对的。这将有更多的,与种子到周期波动的能力,等等。我只是试图削减它的简洁性,同时给出了一个合理的集合方法表示;可能会更清楚。再次感谢。在NotifyPropertyChanged中,如果您首先检查null,您应该首先将此.PropertyChanged复制到函数本地PropertyChangedEventHandler,然后检查-您的方法留下一个(非常小,但存在)在检查null和引发事件之间删除处理程序的争用条件的可能性。在NotifyPropertyChanged中,如果首先检查null,则应首先将此.PropertyChanged复制到函数local PropertyChangedEventHandler,然后检查-您的方法留有(非常小,但存在)在检查null和引发事件之间删除处理程序的争用条件的可能性。
public double Days
{
    get { return _span.Days; }
    set
    {
        double val = value > 0 ? value : 0;

        // TimeSpan is an immutable struct, must store the result of any
        // operations on it
        _span = TimeSpan.FromDays(val);

        this.OnPropertyChanged("Days");
        this.OnPropertyChanged("Span");
    }
}

// This is preferred way for handling property changes
private event PropertyChangedEventHandler propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
    add { this.propertyChanged += value; }
    remove { this.propertyChanged -= value; }
}

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = this.propertyChanged;
    if (null != handler)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}