Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background textblock on windows phone

I want to change background color on TextBlock on Windows phone. Now I've only got a textblock colored without a space near frame. This effect I've got by this code:

<StackPanel Orientation="Horizontal" Background="{Binding Color}">
    <TextBlock Text="{Binding Name}" Margin="12,0,0,0"></TextBlock>
</StackPanel>
like image 520
Krzysztof Madej Avatar asked Dec 15 '22 11:12

Krzysztof Madej


2 Answers

TextBlock doesn't have a background property on its own. You have to put up a background grid or canvas or border or rectangle to full fill it.

<Grid Width="300" Height="100" Background="Blue">
    <TextBlock Name="MyTextBlock" Text="Hello World!" Foreground="Black" />
</Grid>

Instead of grid you can make a rectangle or border.

like image 169
Mani Avatar answered Jan 02 '23 15:01

Mani


You can also change the background color on the getFocus event like

private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
    (sender as TextBox).Background = new SolidColorBrush(Colors.Red);
}
like image 20
Ebscer Avatar answered Jan 02 '23 16:01

Ebscer