在wpf mvvm中绑定时间跨度,并且仅显示分钟:秒?

发布于 2024-11-06 11:29:33 字数 688 浏览 5 评论 0原文

我希望能够在文本框中显示分钟和秒。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-11-13 11:29:33

如果您只想要单向绑定,那么您可以使用 StringFormat:

 <TextBlock Text="{Binding MaxTime, StringFormat={}{0:hh':'mm':'ss}}" />

如果您想要双向绑定,那么我会选择自定义值转换器。

[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':'ss");
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              Globalization.CultureInfo culture)
    {
        // TODO something like:
        return TimeSpan.ParseExact(value, "hh':'mm':'ss", CultureInfo.CurrentCulture);
    }
}

If you just wanted one way binding, then you could use StringFormat:

 <TextBlock Text="{Binding MaxTime, StringFormat={}{0:hh':'mm':'ss}}" />

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':'ss");
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              Globalization.CultureInfo culture)
    {
        // TODO something like:
        return TimeSpan.ParseExact(value, "hh':'mm':'ss", CultureInfo.CurrentCulture);
    }
}
谈下烟灰 2024-11-13 11:29:33

我采取了不同的策略并创建了一个名为 TimeElapsed 的字符串,在计时器滴答期间,我会查看另一个属性,它是 StopWatch (在计时器期间设置),然后如果更改设置在屏幕上显示的字符串时间。

private Stopwatch StopWatch { get; set; }
private double TimeElapsedDouble { get; set; }

private string _TimeElapsed;

public string TimeElapsed
{
    get { return _TimeElapsed; }
    set { _TimeElapsed = value; OnPropertyChanged(nameof(TimeElapsed)); }
}
private void timerTick(object sender, EventArgs e)
{
    var timeElapsedDuration = StopWatch.Elapsed.Duration(); // Get the timespan.

    if (TimeElapsedDouble != timeElapsedDuration.TotalSeconds) // A change happened?
    {
        TimeElapsedDouble = timeElapsedDuration.TotalSeconds;
        TimeElapsed = timeElapsedDuration.ToString(@"mm\:ss"); // Answer is here
    }

}

然后根据需要绑定到控件上即可

<Label Content="{Binding TimeElapsed}" />

I took a different tact and created a string called TimeElapsed and during a timer tick, I would look at another property which was a StopWatch (set during the timer) and then if changed set the string time for showing on the screen.

private Stopwatch StopWatch { get; set; }
private double TimeElapsedDouble { get; set; }

private string _TimeElapsed;

public string TimeElapsed
{
    get { return _TimeElapsed; }
    set { _TimeElapsed = value; OnPropertyChanged(nameof(TimeElapsed)); }
}
private void timerTick(object sender, EventArgs e)
{
    var timeElapsedDuration = StopWatch.Elapsed.Duration(); // Get the timespan.

    if (TimeElapsedDouble != timeElapsedDuration.TotalSeconds) // A change happened?
    {
        TimeElapsedDouble = timeElapsedDuration.TotalSeconds;
        TimeElapsed = timeElapsedDuration.ToString(@"mm\:ss"); // Answer is here
    }

}

Then just bind it to the control as needed

<Label Content="{Binding TimeElapsed}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文