c# wpf 应用程序中的时钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14470768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:10:49 来源:igfitidea点击:
a clock in c# wpf application
提问by Hatem
- i'm working on c# wpf application and i would like to add a clock to my application:
- how to make that clock in my application?
- how to make my application clock not linked to windows clock??
- how to show clock by different styles in my application?
- how to make it contains calender, time zone...etc .. and modify these things through my application itself?
- can i make my time stamp in Database linked to application clock and how to achieve that?
- 我正在开发 c# wpf 应用程序,我想在我的应用程序中添加一个时钟:
- 如何在我的应用程序中制作那个时钟?
- 如何使我的应用程序时钟不链接到 Windows 时钟?
- 如何在我的应用程序中以不同的样式显示时钟?
- 如何使其包含日历、时区...等 .. 并通过我的应用程序本身修改这些内容?
- 我可以将数据库中的时间戳链接到应用程序时钟以及如何实现吗?
回答by sa_ddam213
It would be quite easy to make a clock like this.
制作这样的时钟会很容易。
Here is a small example to get you started
这是一个让您入门的小示例
Xaml:
Xml:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="233" Width="143" Name="UI">
<Grid DataContext="{Binding ElementName=UI}">
<StackPanel>
<TextBlock Text="{Binding CurrentTime}" />
<ComboBox ItemsSource="{Binding TimeZones}" SelectedItem="{Binding SelectedTimeZone}" />
</StackPanel>
</Grid>
</Window>
Code:
代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _currenttime;
private TimeZoneInfo _selectedTimeZone;
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Background);
timer.Interval = TimeSpan.FromSeconds(1);
timer.IsEnabled = true;
timer.Tick += (s, e) =>
{
UpdateTime();
};
}
public List<TimeZoneInfo> TimeZones
{
get { return TimeZoneInfo.GetSystemTimeZones().ToList(); }
}
public string CurrentTime
{
get { return _currenttime; }
set { _currenttime = value; OnPropertyChanged("CurrentTime"); }
}
public TimeZoneInfo SelectedTimeZone
{
get { return _selectedTimeZone; }
set
{
_selectedTimeZone = value;
OnPropertyChanged("SelectedTimeZone");
UpdateTime();
}
}
private void UpdateTime()
{
CurrentTime = SelectedTimeZone == null
? DateTime.Now.ToLongTimeString()
: DateTime.UtcNow.AddHours(SelectedTimeZone.BaseUtcOffset.TotalHours).ToLongTimeString();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
Clock:
时钟:



