1

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.

steven
  • 11
  • 2
  • Also look at here: http://stackoverflow.com/a/6280132/580053 – Dennis Jul 24 '15 at 07:15
  • http://stackoverflow.com/questions/1322451/what-is-the-difference-between-user-control-custom-control-and-component will give you an insight about when to use UserControl and when to use Custom control – Joseph Jul 24 '15 at 07:16
  • I saw this question before. But it did not solve my question. Could you give me a example that in which situation you can only use UserControl but can not use Custom Control?Thanks. – steven Jul 24 '15 at 07:18
  • I know that a UserControl is a composite of controls. But I can achieve the same effect by using Costum Control and just change the ControlTemplate to has a composite of control instead. Is that right? – steven Jul 24 '15 at 07:22
  • As a rule of thumb: start with `UserControl`. It's easier to create one and everything is in one place. Only use custom control if you have something what `UserControl` can't / shouldn't do, e.g. custom layouting / drawing / controlling - are good candidates for custom control. Use `UserControl` if you can achieve functionality by composing several control. Use custom control if you want to mutate exiting **single** control mostly and it's not possible (or doesn't looks feasible) by changing its template. – Sinatr Jul 24 '15 at 08:29
  • @steven, it's a simple composition - `UserControl` version is definitely better. To convince you: try to edit custom control in designer ^^. While you **can** make it custom control it doesn't make any sense because `UserControl` is good enough. If e.g. you will do custom `Paint` to draw something complicated, instead of providing 4 simple `TextBlock`s, then that should be custom control. – Sinatr Jul 27 '15 at 07:05

0 Answers0