Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I set the Width property of a UserControl in the code behind for the window I want it in to Auto?

I have a user control called Project, I have a MainWindow that uses Project.
The issue is that I need Project to have a Windows Forms-like Docking ability, which "Auto" fits in WPF for Width and Height instead of Fill in Windows Forms.
I've tried using Double.NaN to set the Width of Project in the MainWindow and it doesn't work.

My Code:
C#
public MainWindow()
{
            InitializeComponent();
            Current_User_Project = new ProjectControl();
            Current_User_Project.HorizontalAlignment = HorizontalAlignment.Stretch;
            Current_User_Project.VerticalAlignment = VerticalAlignment.Stretch;
            Current_User_Project.Width = Double.NaN;
            Canvas c = new Canvas();
            c.HorizontalAlignment = HorizontalAlignment.Stretch;
            c.VerticalAlignment = VerticalAlignment.Stretch;
            c.Children.Add(Current_User_Project);
            Project_Tab.Content = c;
}
Posted
Updated 9-Apr-14 10:16am
v2
Comments
Sergey Alexandrovich Kryukov 9-Apr-14 16:47pm    
What layout are you trying to achieve with that? Why are you trying to change it during runtime?
—SA

1 solution

Please see my comment to the question. I don't know what you are trying to achieve and why, so, my answer cannot be final, sorry. If you place your control in some container and set HorizontalAlignment = HorizontalAlignment.Stretch;, the value of Width becomes irrelevant.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900