Change TextBlock color with cs (windows phone 7)

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

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

+4
source share
4 answers

First, you need to specify the TextBlock a x:Name attribute, for example. x:Name="myTextBlock" . After you have done this, TextBlock is available in the code, as a field with the same name as in x:Name .

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

 myTextBlock.Foreground = new SolidColorBrush(someColor); 
+6
source

try the following:

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

Since your TextBlock is in a DataTemplate, it [probably] will be bound to the element in the collection. This means that if you bind a color to a property, you will need this property in the element class, and not on the main page. If you want different items to have different colors, you need to add the property to the item class. If you already have a property that does not match the type (Brush), you can use the converter on the binding so as not to add an unnecessary property.

If you do not want to add a property to your product class, the best option is to declare several DataTemplates on the Resources pages, and then replace the templates as necessary:

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

Go to properties in xaml and add

 Foreground="Red" 
0
source

Source: https://habr.com/ru/post/1380987/


All Articles