0

I'm trying to change the color of the background of an object in a UserControl programmatically because I'm using different dictionaries with different properties for each case using binding. I've already done several of these and it works perfectly. The only difference with this .xaml is that it is used as a UserControl that shows up in the page instead of being a page itself.

Here is the code that works properly

<Grid
   Grid.Row="1"
   Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}, Path=HeaderBackgroundColor, Mode=OneWay}">
       <TextBlock
                Margin="30, 40, 30, 13"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Top"
                Foreground="{StaticResource TertiaryColor}"
                FontFamily="{StaticResource PrimaryFont}">
                <TextBlock.Inlines>
                    <Run
                        FontSize="{StaticResource HeaderTitleSmallerFontSize}"
                        Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}, Path=SplashMessageRight, Converter={StaticResource UpperCaseConverter}, Mode=OneWay}" />
        </TextBlock.Inlines>
   </TextBlock>
</Grid>

As you can see Im using

Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}, Path=HeaderBackgroundColor, Mode=OneWay}">

To then set the color of the background programmaticaly like this

        public SolidColorBrush HeaderBackgroundColor
        {
            get
            {
                switch (KioskManager.ManagerInstance.InstanceConfiguration.Type)
                {
                    case KioskManagerConfiguration.ConfigurationType.TukTuk:
                        return (SolidColorBrush)new BrushConverter().ConvertFrom("#383943");
                    default:
                        return new SolidColorBrush(Colors.White);
                }
            }
        }

So like I was saying this is a Page itself on the App, but when I try to to the same thing but in a UserControl that shows up on this page itself it doesn't work , the color just shows as transparent.

This is the code I use in the UserControl

    <Border
        HorizontalAlignment="Stretch"
        VerticalAlignment="Bottom"
        Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}, Path=ProductSuggestionsControlBackgroundColor, Mode=OneWay}"
        CornerRadius="{StaticResource OverlayCornerRadius}">
    </Border>

And then this to change the color depending on the configuration

        public SolidColorBrush ProductSuggestionsControlBackgroundColor
        {
            get
            {
                switch (KioskManager.ManagerInstance.InstanceConfiguration.Type)
                {
                    case KioskManagerConfiguration.ConfigurationType.TukTuk:
                        return (SolidColorBrush)new BrushConverter().ConvertFrom("#d9645b");
                    default:
                        return new SolidColorBrush(Colors.White);
                }
            }
        }

This second bit of code is a UserControl that gets called on the page that I use the first bit of code in.

2
  • Is TextBlock an element in Xamarin? Do you need to change the background color of Page? In addition, you said that "the color just shows as transparent". You can set other colors instead of White. Commented Dec 8, 2022 at 3:11
  • You would be much better off using dynamic styles. There a quite a few problem with these bindings to ancestors as you have already experienced and in addition it's not very elegant either. learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/… Commented Dec 8, 2022 at 23:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.