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.
TextBlockan 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.