Aufgabe Timer in Titel

This commit is contained in:
2025-06-02 16:11:50 +02:00
parent 80d64c9574
commit 3f2bd74438
4 changed files with 123 additions and 2 deletions

View File

@@ -6,7 +6,7 @@
xmlns:local="clr-namespace:FahzeugWPF" xmlns:local="clr-namespace:FahzeugWPF"
mc:Ignorable="d" mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel, IsDesignTimeCreatable=False}" d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel, IsDesignTimeCreatable=False}"
Title="Fahrzeuge" Height="450" Width="800"> Title="{Binding Path=MainWindowTitle}" Height="450" Width="800">
<StackPanel> <StackPanel>
<DataGrid Name="dgTest" <DataGrid Name="dgTest"
CanUserAddRows="False" CanUserAddRows="False"

View File

@@ -1,18 +1,30 @@
using FahrzeugDatenBank; using FahrzeugDatenBank;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Timers;
namespace FahzeugWPF; namespace FahzeugWPF;
class MainWindowViewModel class MainWindowViewModel : ViewModelBase
{ {
private readonly FahrzeugeModell _model; private readonly FahrzeugeModell _model;
private string _mainWindowTitle = "Fahrzeuge";
private System.Timers.Timer _timer = new System.Timers.Timer()
{
Interval = 1000,
};
public MainWindowViewModel(FahrzeugeModell modell) public MainWindowViewModel(FahrzeugeModell modell)
{ {
this._timer.Elapsed += _timer_Elapsed;
this._timer.Start();
this._model = modell; this._model = modell;
this.InitialisiereDasViewModell(); this.InitialisiereDasViewModell();
} }
public string MainWindowTitle { get { return _mainWindowTitle; } set {
SetProperty<string>(ref _mainWindowTitle, value);
} }
public ObservableCollection<Fahrzeug> Fahrzeuge { get; } = new ObservableCollection<Fahrzeug>(); public ObservableCollection<Fahrzeug> Fahrzeuge { get; } = new ObservableCollection<Fahrzeug>();
private async void InitialisiereDasViewModell() private async void InitialisiereDasViewModell()
@@ -23,4 +35,9 @@ class MainWindowViewModel
this.Fahrzeuge.Add(fahrzeug); this.Fahrzeuge.Add(fahrzeug);
} }
} }
private void _timer_Elapsed(object? sender, ElapsedEventArgs e)
{
this.MainWindowTitle = $"Fahrzeuge {DateTime.Now.ToLongTimeString()}";
}
} }

View 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);
}
}

View 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
}