动态更改标签内容

发布于 2024-12-05 17:22:17 字数 1492 浏览 1 评论 0原文

首先,如果这听起来很愚蠢,我深表歉意,但我对 WPF 还很陌生。我正在做一个计时器,我想更改标签以显示剩余时间。我尝试直接更改内容并通过数据绑定到属性。当我执行前者时,程序就会崩溃;当我执行前者时,程序就会崩溃。至于后者,我不太明白它是如何工作的,我环顾四周,我所能做的就是从网络上的代码片段中获取一些提示,但它不起作用,因为我不知道我在做什么我在做,我也不知道我哪里错了。

代码:我在 MainWindow 类上放置了很多东西,这不是很好的代码,但对于我的目的来说已经足够了。当我尝试直接更改内容时,我通过设置由计时器类调用的委托来完成此操作,调用时会执行以下操作:

private void updateTimerLabel()
{
  lblTimer.Content = TimeToGo;
}

其中 TimeToGo 是以下属性:

public String TimeToGo
{ 
   get { return task.TimeToGo.Hours + ":" + 
                task.TimeToGo.Minutes + ":" + task.TimeToGo.Seconds; }            
}

至于绑定尝试,我设置了以下依赖项属性:

public static readonly DependencyProperty TimeToGoProperty = DependencyProperty.Register(
          "TimeToGo", typeof(String), typeof(MainWindow));

并在 XAML 中执行此操作文件:

<Window x:Class="ToDoTimer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ToDoTimer" Height="210" Width="348" 
        DataContext="{Binding RelativeSource={RelativeSource Self}}">    


    <Grid Width="326" Height="180">
        <Label Content="{Binding TimeToGoProperty}"  Height="63" HorizontalAlignment="Left" Margin="12,12,0,104" Name="lblTimer" VerticalAlignment="Center" FontSize="40" Width="218" FontFamily="Courier New" VerticalContentAlignment="Center" />
    </Grid>
</Window>

First of, I apologize if this sounds stupid, but I'm very new to WPF. I'm doing a timer, and I want to change the Label to display the remaining time. I've tried changing the Content directly and by data binding to a property. When I do the former, the program simply crashes; as for the latter I don't really understand how it works, I've looked around and all I could do was take some hints from snippets of code around the web, but it's not working and since I don't know what I'm doing, I don't know where I'm going wrong either.

The code: I'm putting many things on the MainWindow class, it's not great code, but it's good enough for my purpose. When I tried changing the content directly I did it by setting a delegate called by my timer class, when called does this:

private void updateTimerLabel()
{
  lblTimer.Content = TimeToGo;
}

where TimeToGo is the following property:

public String TimeToGo
{ 
   get { return task.TimeToGo.Hours + ":" + 
                task.TimeToGo.Minutes + ":" + task.TimeToGo.Seconds; }            
}

As for the binding attempt I set the following Dependency Property:

public static readonly DependencyProperty TimeToGoProperty = DependencyProperty.Register(
          "TimeToGo", typeof(String), typeof(MainWindow));

and did this in the XAML file:

<Window x:Class="ToDoTimer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ToDoTimer" Height="210" Width="348" 
        DataContext="{Binding RelativeSource={RelativeSource Self}}">    


    <Grid Width="326" Height="180">
        <Label Content="{Binding TimeToGoProperty}"  Height="63" HorizontalAlignment="Left" Margin="12,12,0,104" Name="lblTimer" VerticalAlignment="Center" FontSize="40" Width="218" FontFamily="Courier New" VerticalContentAlignment="Center" />
    </Grid>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

东北女汉子 2024-12-12 17:22:17

这是我没有任何绑定的东西(经过测试并且有效):

DispatcherTimer timer = new DispatcherTimer();
DateTime endDate = new DateTime();
TimeSpan timeToGo = new TimeSpan(0, 1, 0);

public MainWindow()
{
    InitializeComponent();

    this.timer.Tick += new EventHandler(timer_Tick);
    this.timer.Interval = new TimeSpan(0, 0, 1);

    this.endDate = DateTime.Now.Add(timeToGo);

    this.timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    this.lblTimer.Content = this.ToStringTimeSpan(this.endDate - DateTime.Now);

    if (this.endDate == DateTime.Now)
    {
        this.timer.Stop();
    }
}

string ToStringTimeSpan(TimeSpan time)
{
    return String.Format("{0:d2}:{1:d2}:{2:d2}", time.Hours, time.Minutes, time.Seconds);
}

This is what I have without any binding (tested and it's works):

DispatcherTimer timer = new DispatcherTimer();
DateTime endDate = new DateTime();
TimeSpan timeToGo = new TimeSpan(0, 1, 0);

public MainWindow()
{
    InitializeComponent();

    this.timer.Tick += new EventHandler(timer_Tick);
    this.timer.Interval = new TimeSpan(0, 0, 1);

    this.endDate = DateTime.Now.Add(timeToGo);

    this.timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    this.lblTimer.Content = this.ToStringTimeSpan(this.endDate - DateTime.Now);

    if (this.endDate == DateTime.Now)
    {
        this.timer.Stop();
    }
}

string ToStringTimeSpan(TimeSpan time)
{
    return String.Format("{0:d2}:{1:d2}:{2:d2}", time.Hours, time.Minutes, time.Seconds);
}
撩心不撩汉 2024-12-12 17:22:17

您确定您使用的是 DispatcherTimer 而不是 Timer 吗?

Are you sure you are using DispatcherTimer but not Timer?

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