This project has moved and is read-only. For the latest updates, please go here.

ChildWindow in CodeBehind

Dec 2, 2010 at 5:15 PM

Is there a possibility the child window object create in code behind?

The content part ist always empty.

 

Thanks.

Dec 2, 2010 at 7:23 PM

You must set the Content of the ChildWindow control.

//create the window
ChildWindow window = new ChildWindow();

//set the content of the window
window.Content = new TextBlock() { Text = "Hello World" };

//add it to a container control, in this case a Grid named Root
this.Root.Children.Add(window);

//show the window
window.Show();
Feb 12, 2011 at 7:50 PM

'ello

I have noticed some strange behavior using the ChildWindow in code.

If the grid contains rows, the ChildWindow's contents are not displayed.  Also; although i set IsModal to true, the windows are not displayed modally.

below is the code i used. 

Cheerz

Hein

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="Root">
        <Grid Name="Child">
            <Grid.RowDefinitions>
                <RowDefinition Height="24"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="24"/>
            </Grid.RowDefinitions>
            <Menu>
                <MenuItem Header="File">
                    <MenuItem Header="Exit"/>
                </MenuItem>
                <MenuItem Header="Back" />
            </Menu>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <Button Name="btnRoot" Click="btnRoot_Click">Create in Root Grid</Button>
                <Button Name="btnChild" Click="btnChild_Click">Create in Child Grid</Button>
            </StackPanel>
            <StatusBar Grid.Row="2">
                <StatusBarItem Content="Ship Track Trading"/>
            </StatusBar>
        </Grid>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void btnChild_Click(object sender, RoutedEventArgs e)
        {
            ChildWindow c = new ChildWindow();
            c.IsModal = true;
            c.Content = new TextBlock() { Text = "dsfgdfhd" };
            Child.Children.Add(c);
            c.Show();
        }

        private void btnRoot_Click(object sender, RoutedEventArgs e)
        {
            ChildWindow c = new ChildWindow();
            c.IsModal = true;
            c.Content = new TextBlock() { Text = "dsfgdfhd" };
            Root.Children.Add(c);
            c.Show();
        }
    }
}

Feb 13, 2011 at 3:10 AM

You actually discovered a bug with the Modal background.  I have fixed it so you can download the latest source code to get the fix.  As for the first problem with a grid that has rows.  This is the expected behavior. The reason is that the ChildWIndow control is actually part of the visual tree.  Meaning that if you stick it in a row, it will technically be in that row in the visual tree and shrink to fit.  To prove this, just change the size of the first row of 24 to something like 50 and you will see the conent properly.  To fix this either place all child windows in the root grid, or just set the RowSpan property to span all the rows:

        private void btnChild_Click(object sender, RoutedEventArgs e)
        {
            ChildWindow c = new ChildWindow();
            c.WindowStartupLocation = Microsoft.Windows.Controls.WindowStartupLocation.Center;
            c.IsModal = true;
            c.Content = new TextBlock() { Text = "dsfgdfhd" };
            c.SetValue(Grid.RowSpanProperty, 3);
            Child.Children.Add(c);
            c.Show();
        }
Feb 13, 2011 at 6:36 AM

Thanx for the explanation Brian & also for the quick reply & fix

 

Cheerz

Hein

Feb 13, 2011 at 4:53 PM

'ello

Unfortunately it seems that after applying the latest source, the ChildWindows no longer appear centered.

Cheerz

Hein

Feb 13, 2011 at 5:14 PM

This is because of the new WindowStartupLocation property.  If you want the ChildWindow to be centered you must set the WidowStartupLocation = Center.  See previous code snippet.

Feb 13, 2011 at 6:06 PM

Sorry, saw that you added the WindowStartupLocation property.

Mar 8, 2011 at 7:53 AM

I didn' see the property of WindowStartupLocation. I am using 1.3.0.0 version

Mar 8, 2011 at 2:21 PM

Download the latest source code.  It is a new property since release 1.3.0.

Apr 4, 2013 at 5:27 PM
Hi everyone , Good thread !
Childwindow1.SetValue(ShowCloseButtonProperty ,false) seems to be not working
anyone have an idea ?
Apr 4, 2013 at 5:46 PM
fokzia wrote:
Hi everyone , Good thread !
Childwindow1.SetValue(ShowCloseButtonProperty ,false) seems to be not working
anyone have an idea ?
its all right "SelectAccount.CloseButtonVisibility = System.Windows.Visibility.Collapsed;"