在wpf mvvm中绑定时间跨度,并且仅显示分钟:秒?
我希望能够在文本框中显示分钟和秒。 TextBox
绑定到一个属性,即 TimeSpan
。在文本框中,默认值为:“00:00:00”。
这工作正常,但如何仅显示删除小时部分的 00:00。
我该怎么做?
这是我的绑定:
public TimeSpan MaxTime
{
get
{
if (this.Examination.MaxTime == 0)
return TimeSpan.Zero;
else
return maxTime;
}
set
{
maxTime = value;
OnPropertyChanged("MaxTime");
TimeSpan x;
this.Examination.MaxTime = int.Parse(maxTime.TotalSeconds.ToString());
}
}
<TextBox Text="{Binding Path=MaxTime,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
I want to be able to show minutes and seconds in a textbox. The TextBox
is bound towards a property, which is a TimeSpan
. In the textbox, the default is : "00:00:00".
This works fine, but how to show only 00:00 where the hours portion is removed.
How do I do this?
This is my binding:
public TimeSpan MaxTime
{
get
{
if (this.Examination.MaxTime == 0)
return TimeSpan.Zero;
else
return maxTime;
}
set
{
maxTime = value;
OnPropertyChanged("MaxTime");
TimeSpan x;
this.Examination.MaxTime = int.Parse(maxTime.TotalSeconds.ToString());
}
}
<TextBox Text="{Binding Path=MaxTime,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想要单向绑定,那么您可以使用 StringFormat:
如果您想要双向绑定,那么我会选择自定义值转换器。
If you just wanted one way binding, then you could use
StringFormat
:If you want bidirectional binding, then I would go for a custom value converter.
我采取了不同的策略并创建了一个名为 TimeElapsed 的字符串,在计时器滴答期间,我会查看另一个属性,它是 StopWatch (在计时器期间设置),然后如果更改设置在屏幕上显示的字符串时间。
然后根据需要绑定到控件上即可
I took a different tact and created a string called
TimeElapsed
and during a timer tick, I would look at another property which was aStopWatch
(set during the timer) and then if changed set the string time for showing on the screen.Then just bind it to the control as needed