HashCalculator.GUI/MainWindow.xaml.cs
2020-03-22 19:56:16 +01:00

108 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HashCalculator.GUI
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private readonly FileSystemSuggestions _suggestions = new FileSystemSuggestions();
private readonly ViewModel _model = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _model;
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
if (sender is InputBar)
{
var inputValue = _model.Text;
if(inputValue != null)
{
_model.Items = _suggestions.ProvideFileSystemSuggestions(inputValue)
.Prepend(new InputEntry(InputEntryType.Text, inputValue, inputValue));
}
}
}
}
internal class ViewModel : INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler? PropertyChanged;
private void Notify(string name)
{
System.Diagnostics.Debug.WriteLine($"Updating {name}");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private void SetField<X>(ref X field, X value, [CallerMemberName] string? propertyName = null)
{
if (propertyName == null)
{
throw new InvalidOperationException();
}
if (EqualityComparer<X>.Default.Equals(field, value))
{
return;
}
field = value;
Notify(propertyName);
}
#endregion
private IEnumerable<InputEntry>? _items;
public IEnumerable<InputEntry>? Items
{
get => _items;
set => SetField(ref _items, value);
}
private InputEntry? selectedItem;
public InputEntry? SelectedItem
{
get { return selectedItem; }
set { SetField(ref selectedItem, value); }
}
private string? selectedValue;
public string? SelectedValue
{
get { return selectedValue; }
set { SetField(ref selectedValue, value); }
}
private string? _text;
public string? Text
{
get { return _text; }
set { SetField(ref _text, value); }
}
}
}