Bug 42182 - Unable to set ControlTemplate for Custom Control in App.xaml
Summary: Unable to set ControlTemplate for Custom Control in App.xaml
Status: RESOLVED NORESPONSE
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-06-27 08:25 UTC by Akash Kava
Modified: 2017-06-20 12:07 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description Akash Kava 2016-06-27 08:25:19 UTC
I have a custom control derived from "ContentView", and I have some other properties. So far everything works alright when I try to change style in App.xaml (Global implicit style) for control, I can easily change individual properties and they work fine. for example...


    <Style TargetType="ns:FormField">

        <!-- this is working correctly -->
        <Setter Property="LabelColor" Value="Blue"/>
    </Style>


I have a default ControlTemplate set for my ContentView, but if I want to change ControlTemplate in App.xaml, it does not work.

    <Style TargetType="ns:FormField">
        <Setter Property="ControlTemplate">
            <ControlTemplate>
                <Grid>
                   .....
                   .....
                   .....
                </Grid>
            </ControlTemplate>
        </Setter>
    <Style/>

It appears that I am unable to change ControlTemplate. Following is code...


    public class FormField: ContentView{

        public FormField(){
            if(this.ControlTemplate==null){

                 // commenting following line does not apply global template
                 this.ControlTemplate = new ControlTemplate(typeof(MyDefaultGridBasedControlTemplate));
            }
        }
 
        .....
        .....
        .....
     
    }

In WPF and Silverlight, there was generic.xaml where we used to define default ControlTemplate. But looks in Xamarin, there is no such way. I guess in constructor, when I assign ControlTemplate, it overrides resources as local value has highest precedence over resources.

In alternative way, I removed constructor, I still don't get ControlTemplate defined in App.xaml.
Comment 1 Stephane Delcroix 2016-09-08 11:34:03 UTC
this behavior is inherent with the way BindableProperties work in Xamarin.Forms. A bindable property set via style is ignored if that property is already set (i.e. is not default) by the user.
Comment 2 Akash Kava 2016-09-08 12:28:39 UTC
I already know what you are talking, please read correctly, even if I do not define Default template, there is no way to set ControlTemplate property in app.xaml or anywhere except explicitly on the page where control is used.
Comment 3 Stephane Delcroix 2016-09-08 12:55:18 UTC
on one hand you say:

> I have a default ControlTemplate set for my ContentView, but if I want to change ControlTemplate in App.xaml, it does not work.

then you say:

> even if I do not define Default template, there is no way to set ControlTemplate property in app.xaml

Which seems contradictory, but I probably am not reading this correctly.

In addition, we have test showing that you can actually set the ControlTemplate using a Style.

So, please provide the simplest possible sample showcasing this issue. That will help us understand why it doesn't work in your case and help us solve the problem.
Comment 4 Akash Kava 2017-06-19 20:02:44 UTC
What I mean to say is, what is generic.xaml alternative in Xamarin?
Comment 5 Stephane Delcroix 2017-06-20 12:07:27 UTC
With no answer on #c3 for months, I'm closing this issue.

Please reopen when you have a reproduction case to attach to this bug entry.