From my understanding, we can always change a custom control's ControlTemplate to has a customized looking which can has a layout and included many other controls in it. And a UserControl is just a composite of many controls. So it seams that a custom control can do what the UserControl does. So why we still need UserControl? Maybe just because a UserControl is simple to implement?
This is not a question asking the differents between Custom Control and UserControl.
Or Maybe can someone give me an example that in which situation you can only use UserControl but can not use Custom Control instead?
Here is a tesing example: I create both UserProfile UserControl and CustomControl. To me they just looks exactly the same. So why we still need UserControl since CustomControl can do the same thing?
The code is as following:
UserControl xaml:
<UserControl
x:Class="UserControlVsCustomControl.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UserControlVsCustomControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Border BorderBrush="Aqua" BorderThickness="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock FontSize="32" Text="User Profile" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
<TextBlock FontSize="32" Text="Name" Grid.Row="1" Grid.Column="0"/>
<TextBlock FontSize="32" Text="Gender" Grid.Row="2" Grid.Column="0"/>
<TextBlock FontSize="32" Text="Age" Grid.Row="3" Grid.Column="0"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="10"/>
<TextBox Grid.Row="2" Grid.Column="1" Margin="10"/>
<TextBox Grid.Row="3" Grid.Column="1" Margin="10"/>
</Grid>
</Border>
</UserControl>
CustomControl xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UserControlVsCustomControl">
<Style TargetType="local:CustomControl1">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl1">
<Border BorderBrush="Aqua" BorderThickness="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock FontSize="32" Text="User Profile" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
<TextBlock FontSize="32" Text="Name" Grid.Row="1" Grid.Column="0"/>
<TextBlock FontSize="32" Text="Gender" Grid.Row="2" Grid.Column="0"/>
<TextBlock FontSize="32" Text="Age" Grid.Row="3" Grid.Column="0"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="10"/>
<TextBox Grid.Row="2" Grid.Column="1" Margin="10"/>
<TextBox Grid.Row="3" Grid.Column="1" Margin="10"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
MainPage xaml:
<Page
x:Class="UserControlVsCustomControl.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UserControlVsCustomControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="UserControl:" FontSize="36" HorizontalAlignment="Center"/>
<TextBlock Text="CustomControl:" FontSize="36" Grid.Column="1" HorizontalAlignment="Center"/>
<local:MyUserControl1 Height="300" Width="300" Grid.Row="1" VerticalAlignment="Top"/>
<local:CustomControl1 Height="300" Width="300" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top"/>
</Grid>
</Page>
Because my reputation is less than 10 I can't upload an screenshot for the result.From my testing, these two controls looks exactly the same.But I think you should get my point. So what I really want to ask is why we still need UserControl when a CustomControl can do the same thing for you and even more powerful?
So no matter how complicated the UserControl you created, you can always create a corresponding Custom Control with exaclty the same look and same functionalities.Is this conclusion right? And if I have something misunderstood, hope someone can correct me. Thanks in advanced.