JD2022-TU1/main/tools/modules/JD.Guinness.WPF.Module/View/Ps4View.xaml.cs

61 lines
2.4 KiB
C#

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using ActiproSoftware.Windows.Controls.Editors;
using JD.Guinness.WPF.Module.Configuration;
using JD.Guinness.WPF.Module.Model.Ps4;
using JD.Guinness.WPF.Module.ViewModel;
using JD.Oasis.Service.API;
namespace JD.Guinness.WPF.Module.View
{
public partial class Ps4View
{
public Ps4View(Ps4ViewModel viewModel)
{
DataContext = viewModel;
InitializeComponent();
viewModel.Game.SaveAsked += OnSave;
viewModel.Game.ItemFocuser.SubscribeWindow<Ps4Configuration>(this);
viewModel.Game.ItemFocuser.Subscribe<Ps4Trophy>(DataGrid);
viewModel.Game.ItemFocuser.Subscribe<Ps4Zone>(Zones);
viewModel.Game.Settings.Ps4DataGridSettings?.Apply(DataGrid);
}
private void OnSave(GuinnessSettings settings) => settings.Ps4DataGridSettings = new DataGridSettings(DataGrid);
private void OnEditableLostFocus(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement element
&& element.Tag is FrameworkElement displayElement
&& element.Parent is ContentControl parent)
{
element.LostFocus -= OnEditableLostFocus;
parent.Content = displayElement;
}
}
private void OnAutoCompleteBoxSubmitted(object sender, AutoCompleteBoxEventArgs e) => OnEditableLostFocus(sender, e);
private void OnEditablePreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is ContentControl contentControl
&& contentControl.Content is FrameworkElement element
&& element.Tag is AutoCompleteBox editElement
&& element.Parent is ContentControl parent)
{
editElement.Tag = element;
parent.Content = editElement;
// wait for ui display so we can focus the edit element
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
editElement.Text = (editElement.SelectedItem as LocalizedLine)?.Id.ToString() ?? string.Empty;
editElement.Focus();
editElement.LostFocus += OnEditableLostFocus;
}
}
}
}