Aufgabe Timer in Titel
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
xmlns:local="clr-namespace:FahzeugWPF"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel, IsDesignTimeCreatable=False}"
|
||||
Title="Fahrzeuge" Height="450" Width="800">
|
||||
Title="{Binding Path=MainWindowTitle}" Height="450" Width="800">
|
||||
<StackPanel>
|
||||
<DataGrid Name="dgTest"
|
||||
CanUserAddRows="False"
|
||||
|
||||
@@ -1,18 +1,30 @@
|
||||
using FahrzeugDatenBank;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Timers;
|
||||
|
||||
namespace FahzeugWPF;
|
||||
|
||||
class MainWindowViewModel
|
||||
class MainWindowViewModel : ViewModelBase
|
||||
{
|
||||
private readonly FahrzeugeModell _model;
|
||||
private string _mainWindowTitle = "Fahrzeuge";
|
||||
private System.Timers.Timer _timer = new System.Timers.Timer()
|
||||
{
|
||||
Interval = 1000,
|
||||
};
|
||||
|
||||
public MainWindowViewModel(FahrzeugeModell modell)
|
||||
{
|
||||
this._timer.Elapsed += _timer_Elapsed;
|
||||
this._timer.Start();
|
||||
this._model = modell;
|
||||
this.InitialisiereDasViewModell();
|
||||
}
|
||||
|
||||
public string MainWindowTitle { get { return _mainWindowTitle; } set {
|
||||
SetProperty<string>(ref _mainWindowTitle, value);
|
||||
} }
|
||||
|
||||
public ObservableCollection<Fahrzeug> Fahrzeuge { get; } = new ObservableCollection<Fahrzeug>();
|
||||
|
||||
private async void InitialisiereDasViewModell()
|
||||
@@ -23,4 +35,9 @@ class MainWindowViewModel
|
||||
this.Fahrzeuge.Add(fahrzeug);
|
||||
}
|
||||
}
|
||||
|
||||
private void _timer_Elapsed(object? sender, ElapsedEventArgs e)
|
||||
{
|
||||
this.MainWindowTitle = $"Fahrzeuge {DateTime.Now.ToLongTimeString()}";
|
||||
}
|
||||
}
|
||||
|
||||
44
FahzeugWPF/RelayCommand.cs
Normal file
44
FahzeugWPF/RelayCommand.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
namespace FahzeugWPF;
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Input;
|
||||
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
readonly Action<object> _execute = null;
|
||||
readonly Predicate<object> _canExecute = null;
|
||||
|
||||
public RelayCommand(Action<object> execute)
|
||||
: this(execute, null)
|
||||
{
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
|
||||
{
|
||||
if (execute == null)
|
||||
{
|
||||
throw new ArgumentNullException("execute");
|
||||
}
|
||||
|
||||
this._execute = execute;
|
||||
this._canExecute = canExecute;
|
||||
}
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return _canExecute == null ? true : _canExecute(parameter);
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_execute(parameter);
|
||||
}
|
||||
}
|
||||
60
FahzeugWPF/ViewModelBase.cs
Normal file
60
FahzeugWPF/ViewModelBase.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
namespace FahzeugWPF;
|
||||
|
||||
using System.ComponentModel;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
public abstract class ViewModelBase : INotifyPropertyChanged
|
||||
{
|
||||
#region Events
|
||||
|
||||
[field: NonSerialized]
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected void OnPropertyChanged(Expression<Func<object>> expression)
|
||||
{
|
||||
var lambdaExpression = expression as LambdaExpression;
|
||||
|
||||
MemberExpression memberExpression = null;
|
||||
if (lambdaExpression.Body is UnaryExpression)
|
||||
{
|
||||
var unaryExpression = lambdaExpression.Body as UnaryExpression;
|
||||
memberExpression = unaryExpression.Operand as MemberExpression;
|
||||
}
|
||||
else
|
||||
{
|
||||
memberExpression = lambdaExpression.Body as MemberExpression;
|
||||
}
|
||||
|
||||
if (memberExpression != null)
|
||||
{
|
||||
var propertyInfo = memberExpression.Member as PropertyInfo;
|
||||
if (propertyInfo != null)
|
||||
{
|
||||
OnPropertyChanged(propertyInfo.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
{
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
||||
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(field, value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
field = value;
|
||||
OnPropertyChanged(propertyName);
|
||||
return true;
|
||||
}
|
||||
#endregion Events
|
||||
}
|
||||
Reference in New Issue
Block a user