在我的应用程序有以下附加属性在加载ICommand
或UserControl
调用一个Window
:
public static readonly DependencyProperty LoadedCommandProperty = DependencyProperty.RegisterAttached(
"LoadedCommand", typeof(ICommand), typeof(UserControlExtensions), new PropertyMetadata(null, OnLoadedCommandChanged));
private static void OnLoadedCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ContentControl userControl = d as ContentControl;
if (userControl == null)
return;
if (e.NewValue is ICommand)
{
userControl.Loaded += UserControlOnLoaded;
}
}
private static void UserControlOnLoaded(object sender, RoutedEventArgs e)
{
ContentControl userControl = sender as ContentControl;
if (userControl == null)
return;
ICommand command = GetLoadedCommand(userControl);
command.Execute(GetLoadedCommandParameter(userControl));
}
public static void SetLoadedCommand(DependencyObject element, ICommand value)
{
element.SetValue(LoadedCommandProperty, value);
}
public static ICommand GetLoadedCommand(DependencyObject element)
{
return (ICommand) element.GetValue(LoadedCommandProperty);
}
public static readonly DependencyProperty LoadedCommandParameterProperty = DependencyProperty.RegisterAttached(
"LoadedCommandParameter", typeof(object), typeof(UserControlExtensions), new PropertyMetadata(default(object)));
public static void SetLoadedCommandParameter(DependencyObject element, object value)
{
element.SetValue(LoadedCommandParameterProperty, value);
}
public static object GetLoadedCommandParameter(DependencyObject element)
{
return (object) element.GetValue(LoadedCommandParameterProperty);
}
这只是正常工作。在我的意见我称之为一个UserControl
,如:
AttachedProperties:UserControlExtensions.LoadedCommand="{Binding ViewLoadedCommand}"
而在视图模型的ICommand
将被调用。
现在我已经实现了一个功能,我可以在Button
的标题点击一个TabItem
,并在新窗口中打开此TabItem
的内容。 (就像在Visual Studio脱位一个选项卡)。
所以我用下面的代码:
private void OpenInWindowButtonOnClick(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
if (button == null)
return;
TabItem tabItem = button.Tag as TabItem;
if (tabItem == null)
return;
string title = string.Empty;
ContentControl headerContent = tabItem.Header as ContentControl;
if (headerContent != null)
{
title = headerContent.Content.ToString();
}
string workspaceId = tabItem.Tag as string;
TabItem workspaceTab = WorkspaceTab.Items.OfType<TabItem>()
.FirstOrDefault(ti => ti.Tag is string && (string)ti.Tag == workspaceId);
if (workspaceTab != null)
{
WorkspaceWindow workspaceWindow = new WorkspaceWindow(tabItem);
workspaceWindow.Content = workspaceTab.Content;
workspaceWindow.Width = (workspaceTab.Content as FrameworkElement).ActualWidth;
workspaceWindow.Height = (workspaceTab.Content as FrameworkElement).ActualHeight;
workspaceWindow.Title = title;
workspaceWindow.Closing += WorkspaceWindowOnClosing;
workspaceWindow.Show();
workspaceWindows.Add(workspaceId, workspaceWindow);
WorkspaceTab.Items.Remove(workspaceTab);
}
}
这还只是正常工作。
我现在的问题是,如果我打开一个新标签(女巫还包含TabControl
其中TabItem
s有装载-attachedproperty)和移动该标签与上面的代码的新窗口:当我切换到加载的命令不会叫查看使用加载-attachedproperty其中。
如果我调试attachedproperty我可以看到,该OnLoadedCommandChanged正确调用,但UserControlOnLoaded不叫。如果我没有在新窗口中打开TabItem
的UserControlOnLoaded正确调用。
任何想法,为什么子页面的加载事件,如果我移动TabItem
到一个新的Window
不被解雇?
问题是行内WorkspaceTab.Items.Remove(workspaceTab);
这条线将删除TabItem
的TabControl
,似乎WPF这里做某种清理。
我已经解决了这个问题,只是设置Visibility
到隐藏的TabItem
。