Bug 41048 - Setting properties on UI-elements does sometime not override the value from the style
Summary: Setting properties on UI-elements does sometime not override the value from t...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.2.0
Hardware: Macintosh Mac OS
: High normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-05-12 07:58 UTC by Xet
Modified: 2017-07-18 14:56 UTC (History)
10 users (show)

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


Attachments
Sample application (51.24 KB, application/zip)
2016-05-12 07:58 UTC, Xet
Details


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 FIXED

Description Xet 2016-05-12 07:58:17 UTC
Created attachment 15981 [details]
Sample application

When setting values tn properties for some UI-elements (in the code example I use a Label) the value is sometimes disregarded.
It seems to be related to having an implicit style deriving from an explicit style.

Say I have a couple of styles looking like this:

<Style x:Key="StandardLabelStyle" TargetType="Label">
   <Setter Property="TextColor" Value="Red"/>
   <Setter Property="LineBreakMode" Value="TailTruncation"/>
</Style>

<!-- Derived style with bold font -->
<Style x:Key="StandarBoldLabelStyle" TargetType="Label" BasedOn="{StaticResource StandardLabelStyle}">
   <Setter Property="FontAttributes" Value="Bold"/>
</Style>
<!-- Use the StandardLabelStyle as implicit style for all labels -->
<Style TargetType="Label" BasedOn="{StaticResource StandardLabelStyle}"/>

If I create a Label like this:

new Label{
   Style=(Style)Application.Current.Resources["StandarBoldLabelStyle"],
   Text="A really long text to test LineBreakMode...", 
   LineBreakMode = LineBreakMode.WordWrap
},

I would expect the label to have red text color (from the "base style"), bold font (from the style "StandardBoldLabelStyle") and to word wrap (since I said so when creating the label).
However, the text does not word wrap but is tail truncated.

There are similar issues with TextColor and perhaps other properties as well.

This did work in previous versions of Forms, I started to notice it in 2.2.0
I have attached a small sample project that highlights the issues.

It does not seem to be a platform issues, since iOS and Android shows the same symptoms
Comment 1 Clint 2016-07-11 14:33:49 UTC
I was about to report the same thing when I searched and found this post.

To help narrow down the source of the problem I can confirm that it has to do with the ORDER the elements are in, in the XAML.

If you look at these two labels... They are the same Style and both are trying to override the color of that style.  The difference is the first one is successful and the second one isn't.  After a lot of experiments I can confirm that you have to put your override values in the XAML before you reference the style.

```xml
                        <Label BackgroundColor="{StaticResource TileElogs}"
                               HorizontalOptions="FillAndExpand"
                               HorizontalTextAlignment="Start"
                               TextColor="{StaticResource PrimaryTextColor}" 
                               Style="{StaticResource LabelStyleReversedMed}"
                               Text="Elog Notices"
                               />
                        <Label BackgroundColor="{StaticResource TileElogs}"
                               HorizontalOptions="FillAndExpand"
                               HorizontalTextAlignment="End"
                               Style="{StaticResource LabelStyleReversedMed}"
                               Text="{Binding AppVM.DriverDisplayed.ElogsNotifications.Count}"
                               TextColor="{StaticResource PrimaryTextColor}" />
```
Comment 2 Clint 2016-07-11 14:39:16 UTC
WORKAROUND UNTIL FIXED:
Install the VSIX "XAML Styler" to Visual Studio.
Update the options for ordering attributes.
Add 'Style' as the last element on the last block.  Now Style will always be last so your overrides will be effective.

Keystroke to apply XAML Styler is: cntrl+k, cntrol+2
Comment 3 Robert 2016-11-03 21:50:57 UTC
I can confirm that this still exists and the method of moving the Style to the last attribute being defined does not seem to work anymore for me. I am attempting to change the color of a label on a one time basis but want to keep all other styling and this is not working for me. The TextColor property is being ignored and instead the style's value is being grabbed.

I am developing on the latest version of Xamarin using Visual Studio.
Comment 4 Clint 2016-11-04 11:57:55 UTC
Hmmm... I still have the issue, also latest VS updates, latest Xamarin framework, latest Xamarin.Forms NuGet... But putting the Style attribute on the last line still works throughout our solution.

EX:

<Label Grid.Column="1"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand"
        HorizontalTextAlignment="Center"
        VerticalTextAlignment="Center"
        BackgroundColor="Red"
        IsVisible="{Binding MultiPageDocument}"
        Text="{StaticResource Multipage}"
        Style="{StaticResource LabelStyleMed}" />
Comment 5 Clint 2016-11-04 12:27:30 UTC
Tested it specifically with the TextColor property that you mentioned.  That's changing as expected... So long as the Style is set last.

                        <Label Grid.Column="1"
                               HorizontalOptions="FillAndExpand"
                               VerticalOptions="FillAndExpand"
                               HorizontalTextAlignment="Center"
                               VerticalTextAlignment="Center"
                               BackgroundColor="Red"
                               IsVisible="{Binding MultiPageDocument}"
                               Text="{StaticResource Multipage}"
                               TextColor="Yellow"
                               Style="{StaticResource LabelStyleMed}" />
Comment 6 Robert 2016-11-04 15:45:34 UTC
Maybe my issue has to do with my style being based on a device style. I define 

      <Style x:Key="AEBodyStyle"
             TargetType="Label"
             BaseResourceKey="BodyStyle">
        <Setter Property="TextColor" Value="{DynamicResource PrimaryColor}" />      
      </Style>

And my label is defined

                <Label Text="{Binding CurrentTemperature, StringFormat='Current: {0}°F'}" 
                       IsVisible="{Binding IsCurrent}"
                       TextColor="Green"
                       Style="{DynamicResource AEBodyStyle}" />

And my text is still coming out as the primary color which is orange. It might have something to do with the BaseResourceKey, but honestly I just wish the bug would get fixed.
Comment 7 Robert 2016-11-04 16:00:29 UTC
I just realized that the issue is for a dynamic resource. If I bind a dynamic resource, the order does not matter.
Comment 8 Clint 2016-11-04 17:25:48 UTC
@Robert - Good find!
Comment 9 Robert 2016-12-01 17:07:29 UTC
Is this being worked on at all? I see that the bug is confirmed and filed as a normal bug, but I do not see any updates about progress or when this might get fixed.
Comment 10 Clint 2017-01-30 20:20:42 UTC
I just ran across another manifestation of this.

If you make a ContentView with BindableProperties backing it including Style and elements of the style you might override such as color, the binded overrides don't take affect.

Example: My bosses wanted a button that was more Windows like where disabled grayed out was actually gray, not a dark version of the enabled color.  So I made a WinButton:ContentView and give it a bunch of BindableProperties for text color, background color, text and so on, including Style.

Using it on the page like this:
<controls:WinButton HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand"
                    BackgroundColor="#9CD729"
                    Command="{Binding ApproveScanCommand}"
                    IsEnabled="{Binding IsAcceptable}"
                    IsVisible="{Binding AutoAccept,
                                        Converter={StaticResource BoolInvertConverter}}"
                    Text="{StaticResource Accept}"
                    TextColor="Black"
                    Style="{StaticResource LabelStyleMed}"/>

The textcolor of black does not overright the color defined in the LabelStyleMed.
Remove the Style from this markup and the label text is black as expected.

The WinButton markup does take into account the work-around of having Style listed last.  But I guess once it all generated down that's getting lost.

        <Label Margin="10,3,10,3"
               HorizontalOptions="Center"
               VerticalOptions="Center"
               VerticalTextAlignment="Center"
               BackgroundColor="{Binding Source={x:Reference this},
                                         Path=TextBackground}"
               Text="{Binding Source={x:Reference this},
                              Path=Text}"
               TextColor="{Binding Source={x:Reference this},
                                   Path=TextColor}"
               Style="{Binding Source={x:Reference this},
                               Path=Style}" />
Comment 11 Heinrich Moser 2017-03-27 10:46:28 UTC
This issue can be reproduced solely with implicit styles as well:

App.xaml:
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="{x:Type Label}">
                <Setter Property="TextColor" Value="Black" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>

MainPage.xaml:
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="{x:Type Label}">
                <Setter Property="TextColor" Value="Blue" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>
        <Label Text="ABCDEF" TextColor="Red" />
    </StackLayout>


Expected result: The text is red.
Actual result: The text is blue.

Removing the either the global style or the page-level style fixes the issue.

The workaround of putting the Style attribute at the end obviously does not work here, since there is no Style attribute.
Comment 12 Stephane Delcroix 2017-04-06 10:02:06 UTC
this is related, but slightly different, to https://bugzilla.xamarin.com/show_bug.cgi?id=54334
Comment 13 Rui Marinho 2017-04-06 21:20:03 UTC
Should be fixed on 2.3.4-sr1 or 2.3.5-pre1
Comment 14 Clint 2017-07-17 20:00:43 UTC
> Should be fixed on 2.3.4-sr1 or 2.3.5-pre1

Nope.  Still there in -pre6
Comment 15 Stephane Delcroix 2017-07-18 09:22:55 UTC
I just verified that both the original report (with the original reproduction case) and  the report in #c11 are indeed fixed in 2.3.4.247 (-sr2) and 2.3.5.236 (-pre6)
Comment 16 David Ortinau [MSFT] 2017-07-18 14:56:46 UTC
Clint, please open a new bugzilla with your reproduction case so we can investigate it independent of this issue.

Dave