using System; using System.Collections.Generic; using System.Windows.Controls; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.CommandWpf; using GalaSoft.MvvmLight.Messaging; using HandyControlDemo.Data; using HandyControlDemo.Tools; namespace HandyControlDemo.ViewModel { public class MainViewModel : ViewModelBase { #region 字段 /// /// 内容标题 /// private object _contentTitle; /// /// 子内容 /// private object _subContent; /// /// 数据列表 /// private List _dataList; #endregion public MainViewModel() { Messenger.Default.Register(this, MessageToken.LoadShowContent, obj => SubContent = obj); var list = new List(); for (var i = 1; i <= 6; i++) { var dataList = new List(); for (int j = 0; j < 3; j++) { dataList.Add(new DemoDataModel { Index = j, IsSelected = j % 2 == 0, Name = $"SubName{j}", Type = (DemoType)j }); } var model = new DemoDataModel { Index = i, IsSelected = i % 2 == 0, Name = $"Name{i}", Type = (DemoType)i, DataList = dataList }; list.Add(model); } DataList = list; } #region 属性 /// /// 数据列表 /// public List DataList { get => _dataList; set => Set(ref _dataList, value); } /// /// 子内容 /// public object SubContent { get => _subContent; set => Set(ref _subContent, value); } /// /// 内容标题 /// public object ContentTitle { get => _contentTitle; set => Set(ref _contentTitle, value); } #endregion #region 命令 /// /// 切换例子命令 /// public RelayCommand SwitchDemoCmd => new Lazy>(() => new RelayCommand(SwitchDemo)).Value; #endregion #region 方法 /// /// 切换例子 /// private void SwitchDemo(SelectionChangedEventArgs e) { if (e.AddedItems.Count == 0) return; if (e.AddedItems[0] is ListBoxItem item) { if (item.Tag is string tag) { if (item.Content.Equals(ContentTitle)) return; ContentTitle = item.Content; Messenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControl.{tag}"), MessageToken.LoadShowContent); } else { ContentTitle = null; SubContent = null; } } } #endregion } }