Karloss
Karloss

Reputation: 817

Changing TextBlock color from cs (windows phone 7)

I am completely new developer at Windows Phone 7. I have TextBlock Text="{Binding MyDate} on MainPage.xaml page.

How can I change color of this TextBlock from MainPage.xaml.cs?

Upvotes: 4

Views: 14238

Answers (4)

Pushpendra Paliwal
Pushpendra Paliwal

Reputation: 1

Go in the properties in xaml and add

Foreground="Red"

Upvotes: 0

calum
calum

Reputation: 1580

As your TextBlock is in a DataTemplate it will [probably] be binding to an item in a collection. This means if you bind the color to a property you will need that property in the item class and not in your main page. If you want different items to be different colors then you will need to add a property to the item class. If you already have a property that isn't the correct type (Brush) then you can use a converter on the binding to avoid adding an unnecessary property.

If you don't want to add the property to your item class then the best option is to declare multiple DataTemplates in the pages Resources and then swap the templates as you need:

something.ItemTemplate = (DataTemplate)this.Resources["BlueItemTemplate"];

Upvotes: 0

Luke Woodward
Luke Woodward

Reputation: 64949

Firstly, you need to give the TextBlock element an x:Name attribute, e.g. x:Name="myTextBlock". Once you've done this, the TextBlock is available in the code-behind as a field with the same name as what you put in the x:Name.

To set the foreground color of a TextBlock, you can use something like

myTextBlock.Foreground = new SolidColorBrush(someColor);

Upvotes: 6

aarti
aarti

Reputation: 175

try this :

<Grid  Background="Yellow" >  
    <TextBlock Foreground="Blue"
               Height="20"
               HorizontalAlignment="Stretch"
               Margin="0" 
               Text="this is a test"/> 
</Grid>

Upvotes: 0

Related Questions