using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using PersonalToolBox.ViewModels;
namespace PersonalToolBox.Views;
public partial class MainWindow : Window
{
private readonly MainViewModel _viewModel;
public MainWindow(MainViewModel viewModel)
{
InitializeComponent();
_viewModel = viewModel;
DataContext = viewModel;
viewModel.Logs.CollectionChanged += OnLogsCollectionChanged;
}
private void OnLogsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && LogListBox.Items.Count > 0)
{
Dispatcher.BeginInvoke(new Action(() =>
{
LogListBox.ScrollIntoView(LogListBox.Items[^1]);
}), System.Windows.Threading.DispatcherPriority.Background);
}
}
}
///
/// bool → double: true=1.0, false=0.4 (工具卡片失效时置灰)
///
public class BoolToOpacityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is bool b && b ? 1.0 : 0.4;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
///
/// string → string: 取字符串首字符作为图标占位
///
public class FirstCharConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = value as string;
return string.IsNullOrEmpty(s) ? "?" : s[0].ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}