60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
using HeBianGu.Base.WpfBase;
|
|
using HeBianGu.Control.Explorer;
|
|
using Hopetry.Provider.Behaviors;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
|
|
namespace HeBianGu.App.Disk
|
|
{
|
|
/// <summary>
|
|
/// ExplorerControl.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class ExplorerControl : UserControl
|
|
{
|
|
public ExplorerControl()
|
|
{
|
|
InitializeComponent();
|
|
this.Loaded += OnLoaded;
|
|
}
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
// 查找h:Explorer控件
|
|
var explorer = FindVisualChild<Explorer>(this);
|
|
|
|
if (explorer != null && this.DataContext is LoyoutViewModel viewModel)
|
|
{
|
|
// 获取ExplorerBehavior并传递给ViewModel
|
|
var behavior = Interaction.GetBehaviors(explorer)
|
|
.OfType<ExplorerMinIOBehavior>()
|
|
.FirstOrDefault();
|
|
|
|
viewModel.SetExplorerBehavior(behavior);
|
|
}
|
|
}
|
|
|
|
// 辅助方法:在可视化树中查找指定类型的子元素
|
|
private static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
|
|
{
|
|
if (parent == null) return null;
|
|
|
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
|
|
{
|
|
var child = VisualTreeHelper.GetChild(parent, i);
|
|
if (child is T result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var childResult = FindVisualChild<T>(child);
|
|
if (childResult != null)
|
|
{
|
|
return childResult;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|