UWP User Control Question

karl rock 6 Reputation points
2020-08-23T20:59:34.793+00:00

I don't know if this is the right place to ask this question but I am sort of desperate because I can't find this simple thing anywhere so here I go:

I am trying to create an UWP User Control for the first time. One very basic thing I want is automatic Width and Height based on the content of the user control. Simple enough in theory, but whenever I build the control and drop it on a form, it has this default "Width=100" value. Same thing with the Height.

Here is the simple XAML code i have so far:

    <UserControl
    x:Class="JTE.JTreeItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:JTE"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Width="Auto"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>
        <TextBlock HorizontalAlignment="Left" Margin="10,32,0,0" Text="TextBlock" TextWrapping="Wrap" VerticalAlignment="Top"/>

    </Grid>
</UserControl>

But when I drag the control to a page this is what I get:

<local:JTreeItem HorizontalAlignment="Left" Height="100" Margin="247,305,0,0" VerticalAlignment="Top" Width="100"/>

Both the Width and the Height are 100 by default. But why? How can I change this? This seems so simple, yet adding:

Width="Auto"

Does not solve this. Removing it does not solve it. Any value I put there gets ignored, for example, if I do this:

Width="199"

Nothing happens. I get the same 100 default value for the Width and the Height.

Why is this so not intuitive? There seems to be no syntax errors in the XAML, so what is happening there?

I am using Visual Studio Community 2019.

If any of you can help, I would appreciate it. Thanks in advance!!!

Universal Windows Platform (UWP)
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.