动态更改标签内容
首先,如果这听起来很愚蠢,我深表歉意,但我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我没有任何绑定的东西(经过测试并且有效):
This is what I have without any binding (tested and it's works):
您确定您使用的是 DispatcherTimer 而不是 Timer 吗?
Are you sure you are using DispatcherTimer but not Timer?