Reputation: 161
I'm having an issue setting the Height and Width in a UserControl. I create a new WPF application and it automatically creates MainWindow.xaml. The code looks like this:
//MainWindow.xaml
<Window x:Class="Project.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>
</Grid>
</Window>
//MainWindow.cs
namespace Project
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
I change the code so that I'm using a UserControl instead of a Window, like so:
//MainWindow.xaml
<UserControl x:Class="Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<Grid>
</Grid>
</UserControl>
//MainWindow.cs
namespace Project
{
public partial class MainWindow : UserControl
{
public MainWindow()
{
InitializeComponent();
}
}
}
Why wouldn't the Height or Width be getting set? It can't be that difficult.
Upvotes: 2
Views: 6848
Reputation: 24453
The code in your second sample shouldn't even compile as you've specified a different base class in your code-behind (Window) as your XAML (UserControl).
Assuming you change the code-behind to UserControl (or nothing as it will actually pick it up from just the XAML) the size should get picked up from what you have set in the XAML, but only as initial defaults. To actually display your control somewhere and instance is going to be created, probably in XAML but could also be from code. Each of those instances will be created with the Width and Height that you have set here but at any point those can be overridden:
<local:MainWindow Width="25" Height="100"/>
At that point the values you had set are now gone. There are also many different interactions that can take place within the layout system depending on what's around the control instance. The containing Panel or other element, along with Alignment, Margin and various other settings can affect the actual rendered size of your control.
Upvotes: 2
Reputation: 13763
(Pardon if I make a nomenclature mistake, I'm still learning WPF. Please do correct me if I'm wrong about this.)
UserControls need to be added in some form of container (StackPanel, Grid, Window, ...). Those containers are the ones that usually set up the layout, and most of them do take into account a set Width/height of their child elements. However, without a containing parent, the child doesn't get its layout set up properly.
If you think about it, how whould you want your usercontrol (which is on the same level as a button (although a very complicated button, at that)) to be shown to the user, if not in a Window?
Of course it's nice to be able to create separate files for each UserControl, but in the end, you'll always have to load it in a Window (correct me if I'm wrong).
So instead of removing the main Window, why not just create a new UserControl in a separate file?
Upvotes: 0