0

我的 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>

新聊天窗口.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;
        }
    }
4

1 回答 1

0

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

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);
        }));
于 2013-10-20T18:00:12.033 回答