【问题标题】:Getting the parent of usercontrol inside Dispatcher.BeginInvoke在 Dispatcher.BeginInvoke 中获取 usercontrol 的父级
【发布时间】:2013-10-21 13:02:30
【问题描述】:

我的 WPF 应用程序中有一个主窗口,其中有一个 tabcontrol。我需要根据我传递的名称选择 tabitem。每个 tabitem 都将包含一个用户控件。但我的问题是无法使用 Parent 属性获取用户控件的父级。它始终只返回 null。谁能帮帮我。

public void selectingTab()
    {
        Dispatcher.BeginInvoke((Action)(() =>
        {
            string name = todisplayChat.GetValue<string>("FromMessenger");
            string msg = todisplayChat.GetValue<string>("ChatSent");
            NewChatWindow childWindow = this as NewChatWindow;
            MainChatWindow parentWindow = VisualTreeHelper.GetParent(childWindow) as MainChatWindow;

            //Am getting null as the value of parentWindow after the execution of this line

            TabControl tabcontrolHome = parentWindow.tabcontrol_Chatwindow;
            TabItem existingTab = tabcontrolHome.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == name);
            childWindow = (NewChatWindow)existingTab.Content;
            childWindow.richtxtbox_chatwindow.AppendText(Environment.NewLine + name + " : " + msg);
        }));
    }

MainChatWindow.XAML

<Window x:Class="Ping.MainChatWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="700" Icon="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\ping_logo.png" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" WindowStyle="None" Foreground="{x:Null}" BorderBrush="#FF87A4DB" BorderThickness="2">
<Grid>
    <Label Content="Ping" Height="30" HorizontalAlignment="Left" Name="lbl_Titlebar" VerticalAlignment="Top" Width="700" Background="#FF87A4DB" FontFamily="Arial" FontSize="17" Foreground="White" />
    <Image Height="20" HorizontalAlignment="Left" Margin="635,5,0,0" Name="img_Minimizebutton" Stretch="Fill" VerticalAlignment="Top" Width="20" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\minimize_userpage.png" />
    <!--<Label Height="25"   HorizontalAlignment="Left" Margin="665,5,0,0" VerticalAlignment="Top" Width="20">-->
        <Image MouseEnter="close_icon_MouseEnter_1" MouseDown="close_icon_MouseDown_1" Height="20"  HorizontalAlignment="Left" Margin="665,5,0,0" Name="close_icon" Stretch="Fill" VerticalAlignment="Top" Width="20" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\close_userpage.png" />
    <!--</Label>-->

    <Label Content="Groups" Height="28" HorizontalAlignment="Left" Margin="12,46,0,0" Name="lbl_Groups" VerticalAlignment="Top" Background="#FF87A4DB" Foreground="White" BorderBrush="{x:Null}" Width="245" FontSize="16" FontFamily="Arial" />
    <RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="175" HorizontalAlignment="Left" Margin="12,73,0,0" Name="richtxtbox_Groups" VerticalAlignment="Top" Width="245" FontFamily="Arial" />
    <Label Background="#FF87A4DB" BorderBrush="{x:Null}" Content="Online Buddies" FontFamily="Arial" FontSize="16" Foreground="White" Height="28" HorizontalAlignment="Left" Margin="12,260,0,0" Name="lbl_Onlinebuddies" VerticalAlignment="Top" Width="245" />
    <Grid ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="298" HorizontalAlignment="Left" Margin="12,288,0,0" Name="richtxtbox_Onlinebuddies" VerticalAlignment="Top" Width="245"></Grid>
    <!--<RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="298" HorizontalAlignment="Left" Margin="12,288,0,0" Name="richtxtbox_Onlinebuddies" VerticalAlignment="Top" Width="245" FontFamily="Arial" />-->
    <TabControl Height="540" HorizontalAlignment="Left" Margin="274,46,0,0" Name="tabcontrol_Chatwindow" VerticalAlignment="Top" Width="410">
        <TabItem Header="My Profile" Name="ProfileTab">

        </TabItem>
    </TabControl>

</Grid>

NewChatWindow.Xaml

<UserControl x:Class="Ping.NewChatWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="540" d:DesignWidth="410">

<Grid>     

    <TextBox LostFocus="txt_Userinput_LostFocus_1" Foreground="Gray" GotFocus="txt_Userinput_GotFocus_1" KeyDown="txt_Userinput_KeyDown_1" Height="65" HorizontalAlignment="Left" Margin="13,161,0,0" Name="txt_Userinput" VerticalAlignment="Top" Width="378" FontFamily="Arial" Text="Enter Your Words Here" />
    <RichTextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Height="246" HorizontalAlignment="Left" Margin="13,253,0,0" Name="richtxtbox_chatwindow" VerticalAlignment="Top" Width="378" FontFamily="Arial" />
    <Image Height="93" HorizontalAlignment="Left" Margin="36,36,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="65" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\user_logo.png"/>
    <Label Content="Label" Height="37" HorizontalAlignment="Left" Margin="124,24,0,0" Name="lbl_Username" VerticalAlignment="Top" Width="278" FontSize="24" />
    <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="125,52,0,0" Name="lbl_Statusmsg" VerticalAlignment="Top" Width="284" />
    <Image Height="40" Width="30" HorizontalAlignment="Left" Margin="131,89,0,0" Name="img_Profileview" Stretch="Fill" VerticalAlignment="Top"  Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\user_logo.png" ToolTip="Add contact"/>
    <Image MouseDown="img_Sendingfile_MouseDown_1" Height="40" Width="40" HorizontalAlignment="Left" Margin="191,89,0,0" Name="img_Sendingfile" Stretch="Fill" VerticalAlignment="Top" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\sendingfile_icon.png" ToolTip="Fileupload"/>
    <Image Height="40" Width="40" HorizontalAlignment="Left" Margin="261,89,0,0" Name="img_Videochat" Stretch="Fill" VerticalAlignment="Top" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\videochat_logo.png" ToolTip="Video chat"/>
    <Image MouseDown="img_Myfolder_MouseDown_1" Height="40" Width="40" Name="img_Myfolder" Stretch="Fill" Source="F:\Dhivya\C#\Personal\ChatApplication\CurrentlyWorking-Important\Ping\Ping\images\myfolder_logo.png" Margin="329,89,41,411" ToolTip="View chat history" />
    <Border BorderBrush="#FF87A4DB" BorderThickness="1" HorizontalAlignment="Left" Margin="10,18,0,367" Name="border_Userdetails" Width="378" CornerRadius="2" />
</Grid>

我有一组在线用户的标签。所以当我双击任何标签时,我会执行以下操作

private void Label_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
    {
        Label selectedLabel = (Label)sender;
        TabItem existingTab = tabcontrol_Chatwindow.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == selectedLabel.Content);
        if (tabcontrol_Chatwindow.Items.Contains(existingTab))
        {
            tabcontrol_Chatwindow.SelectedItem = existingTab;
        }
        else
        {
            TabItem newTab = new TabItem();
            newTab.Name = selectedLabel.Content.ToString();
            TabHeader header = new TabHeader();
            header.userName.Content = selectedLabel.Content;
            newTab.Header = header;
            //newTab.Header = selectedLabel.Content;
            newTab.Content = new NewChatWindow();
            tabcontrol_Chatwindow.Items.Add(newTab);
            tabcontrol_Chatwindow.SelectedItem = newTab;
        }
    }

【问题讨论】:

  • 如果用直接转换替换 'as' 会发生什么?
  • MainChatWindow parentWindow =(MainChatWindow)VisualTreeHelper.GetParent(childWindow);即使我这样做只会得到空值。
  • 为什么需要将“this”转换为NewChatWindow(使用“as”关键字)?我闻起来很腥......
  • NewChatWindow 的类型是 Usercontrol 还是 Window?
  • 我需要访问 MainChatWindow 中当前存在的 tabitems tats wat 我的期望是。任何人都可以帮助我

标签: c# wpf user-controls dispatcher


【解决方案1】:

尝试使用下面的代码,您可以在其中找到使用Window parentWindow = Window.GetWindow(this); 托管您的UsercontrolWindow

Dispatcher.BeginInvoke((Action)(() =>
        {
            string name = todisplayChat.GetValue<string>("FromMessenger");
            string msg = todisplayChat.GetValue<string>("ChatSent");
            NewChatWindow childWindow = this as NewChatWindow;
            MainChatWindow parentWindow =  Window.GetWindow(this) as MainChatWindow;



            TabControl tabcontrolHome = parentWindow.tabcontrol_Chatwindow;
            TabItem existingTab = tabcontrolHome.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == name);
            childWindow = (NewChatWindow)existingTab.Content;
            childWindow.richtxtbox_chatwindow.AppendText(Environment.NewLine + name + " : " + msg);
        }));

【讨论】:

  • 那么我也只能为空
  • 能分享一下MainChatWindow和NewChatWindow的xaml吗?
  • 你能在上面的函数中检查你的 UserControl 的 this.Parent 是什么吗?
  • 如果 this.Parent 为 null,这意味着您的控件没有逻辑父级,您能告诉您如何将此控件添加到 MainChatWindow 中吗?
  • 哥们,我需要看看你的代码,你是如何将此控件添加到 tabItem 的,以及你在哪里以及如何调用上述方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
  • 2013-03-02
  • 1970-01-01
相关资源
最近更新 更多