在新窗口中打开TabItem

时间:2019-02-11 05:38:31

标签: c# wpf attached-properties

在我的应用程序中,当加载了ICommandUserControl时,我具有以下附加属性可以调用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}"

然后将调用viewmodel中的ICommand

现在,我实现了一项功能,可以单击Button标头中的TabItem,然后在新窗口中打开此TabItem的内容。 (就像在视觉工作室中取消停靠一个标签页一样。)

因此,我正在使用以下代码:

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具有已加载的附加属性),然后使用上面的代码将此标签页移动到新窗口:当我切换到使用已加载属性的视图时,将不会调用该加载命令。

如果我调试附加属性,可以看到正确调用了OnLoadedCommandChanged,但未调用UserControlOnLoaded。如果我没有在新窗口中打开TabItem,则会正确调用UserControlOnLoaded。

有什么主意,如果我将TabItem移到新的Window时为什么子页面的加载事件不会被触发?

1 个答案:

答案 0 :(得分:0)

问题出在WorkspaceTab.Items.Remove(workspaceTab);

行内

此行从TabItem中删除了TabControl,看来wpf在这里做了某种清理。

我已经解决了仅将Visibility中的TabItem设置为“隐藏”的问题。