using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace MainShell.Converter
{
    public class ScreenToViewModelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            var viewModelType = value.GetType();
            var viewName = viewModelType.FullName.Replace("ViewModel", "View");
            var viewType = Type.GetType(viewName);

            if (viewType != null)
            {
                // 创建View实例，并设置其DataContext为当前的ViewModel
                var view = (FrameworkElement)Activator.CreateInstance(viewType);
                view.DataContext = value;
                return view;
            }

            return null;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
