绑定在WPF MVVM时间跨度,而只显示分钟:秒? [英] Binding timespan in wpf mvvm, and show only minutes:seconds?
问题描述
我希望能够设置分和秒在一个文本框。我现在只是我的文本框绑定朝着属性,它是一个时间跨度属性。所以,现在在我的文本框,默认为:00:00:00
这工作得很好,但我希望能够只是有00:00。这样的时间被删除。
我如何做到这一点? !我搜索网,但无法找到什么好的解决办法
感谢
这是我的绑定:
公开时间跨度MAXTIME
{
得到
{
如果(this.Examination .MaxTime == 0)
返回TimeSpan.Zero;
,否则
返回MAXTIME;
}
组
{
MAXTIME =价值;
OnPropertyChanged(MAXTIME);
时间跨度X;
this.Examination.MaxTime = int.Parse(maxTime.TotalSeconds.ToString());
}
}
<文本框高度=23的HorizontalAlignment =左保证金=215,84,0,0文本={绑定路径= MAXTIME,UpdateSourceTrigger = PropertyChanged的,模式=双向}VerticalAlignment = 顶级WIDTH =50/>
如果你只是想一个方法绑定,那么你可以使用的StringFormat
:
< TextBlock的文本={结合MAXTIME,的StringFormat = {} {0:hh\:毫米}}/>
如果你想双向绑定,然后我会去一个自定义值转换器。
[ValueConversion(typeof运算(时间跨度),typeof运算(字符串))]
公共类HoursMinutesTimeSpanConverter:的IValueConverter
{
酒店的公共对象转换(对象的值,类型TARGETTYPE,对象参数,
Globalization.CultureInfo文化)
{
// TODO是这样的:
回报率((时间跨度)值)。的ToString(hh\:毫米);
}
公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,
Globalization.CultureInfo文化)
{
// TODO是这样的:
返回TimeSpan.ParseExact(价值HH:\mm,CultureInfo.CurrentCulture);
}
}
I want to be able to set minutes and seconds in a textbox. I now just bind my textbox towards a property, which is a TimeSpan property. So now in my textbox, default is : 00:00:00.
This works fine, but i want to be able to just have 00:00. So that the hours are removed.
How do i do this? I searched the net but can't find any good solution
Thanks!
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 Height="23" HorizontalAlignment="Left" Margin="215,84,0,0" Text="{Binding Path=MaxTime,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" VerticalAlignment="Top" Width="50" />
If you just wanted one way binding, then you could use StringFormat
:
<TextBlock Text="{Binding MaxTime, StringFormat={}{0:hh\:mm}}" />
If you want bidirectional binding, then I would go for a custom value converter.
[ValueConversion(typeof(TimeSpan), typeof(String))]
public class HoursMinutesTimeSpanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
Globalization.CultureInfo culture)
{
// TODO something like:
return ((TimeSpan)value).ToString("hh\:mm");
}
public object ConvertBack(object value, Type targetType, object parameter,
Globalization.CultureInfo culture)
{
// TODO something like:
return TimeSpan.ParseExact(value, "hh:\mm", CultureInfo.CurrentCulture);
}
}
这篇关于绑定在WPF MVVM时间跨度,而只显示分钟:秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!