using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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;
using Xceed.Wpf.Toolkit.Primitives;
namespace CtrEditor.ObjetosSim.UserControls
{
///
/// Interaction logic for TreeListControlOS.xaml
///
public partial class TreeListControlOS : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
nameof(ItemsSource), typeof(ObservableCollection), typeof(TreeListControlOS), new PropertyMetadata(null, OnItemsSourceChanged));
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
nameof(SelectedItem), typeof(TreeItemViewModel), typeof(TreeListControlOS), new PropertyMetadata(null, OnSelectedItemChanged));
public event EventHandler SelectionChanged;
public ObservableCollection ItemsSource
{
get => (ObservableCollection)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public TreeItemViewModel SelectedItem
{
get => (TreeItemViewModel)GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
public ObservableCollection Items { get; set; } = new ObservableCollection();
public TreeListControlOS()
{
InitializeComponent();
DataContext = this;
}
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TreeListControlOS control)
{
control.LoadItems((ObservableCollection)e.NewValue);
}
}
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TreeListControlOS control && e.NewValue is TreeItemViewModel newItem)
{
// Update the TreeView selection if necessary
control.UpdateTreeViewSelection(newItem);
control.OnSelectionChanged(new SelectionChangedEventArgs(newItem));
}
}
private void UpdateTreeViewSelection(TreeItemViewModel selectedItem)
{
if (treeView != null && selectedItem != null)
{
TreeViewSelectedItemExBehavior.SetSelectedItemEx(treeView, selectedItem);
}
}
public TreeItemViewModel FindViewModelForItem(osBase item)
{
foreach (var i in Items)
{
if (i.Item == item)
return i;
foreach (var child in i.Children)
if (child.Item == item)
return child;
}
return null;
}
private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs