67 lines
1.7 KiB
C#
67 lines
1.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Input;
|
||
using System.Windows.Threading;
|
||
using System.Windows;
|
||
|
||
namespace BeWo.ViewModel.View.Developer
|
||
{
|
||
public class DeveloperViewModel : WindowViewModel
|
||
{
|
||
private string _keyboardFocus;
|
||
private string _logicalFocus;
|
||
|
||
public string KeyboardFocus
|
||
{
|
||
get => _keyboardFocus;
|
||
set => SetProperty(ref _keyboardFocus, value, nameof(KeyboardFocus));
|
||
}
|
||
|
||
public string LogicalFocus
|
||
{
|
||
get => _logicalFocus;
|
||
set => SetProperty(ref _logicalFocus, value, nameof(LogicalFocus));
|
||
}
|
||
|
||
public DeveloperViewModel()
|
||
{
|
||
var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(200) };
|
||
timer.Tick += (s, e) => Refresh();
|
||
timer.Start();
|
||
}
|
||
|
||
private void Refresh()
|
||
{
|
||
// Keyboard Focus – globaler Input-Fokus, nur ein Element systemweit
|
||
var keyboard = Keyboard.FocusedElement;
|
||
KeyboardFocus = FormatElement(keyboard);
|
||
|
||
// Logical Focus – pro FocusScope, kann vom Keyboard Focus abweichen
|
||
// z.B. wenn ein Menü oder Toolbar aktiv ist
|
||
var mainWindow = Application.Current.MainWindow;
|
||
var logical = mainWindow != null
|
||
? FocusManager.GetFocusedElement(mainWindow)
|
||
: null;
|
||
LogicalFocus = FormatElement(logical);
|
||
}
|
||
|
||
private static string FormatElement(object element)
|
||
{
|
||
if (element is null)
|
||
return "(kein Fokus)";
|
||
|
||
if (element is FrameworkElement fe)
|
||
{
|
||
var name = string.IsNullOrEmpty(fe.Name) ? "(kein Name)" : fe.Name;
|
||
var dc = fe.DataContext?.GetType().Name ?? "–";
|
||
return $"{fe.GetType().Name} | Name: {name} | DC: {dc}";
|
||
}
|
||
|
||
return element.GetType().Name;
|
||
}
|
||
}
|
||
}
|