Answered by:
change all textblock in a page with one color dynamically metro app

Question
-
i have a combo box with 4 colors so how do i change all the text in a page with a selected color at runtime?
19karabo91
Tuesday, August 27, 2013 5:50 AM
Answers
-
xaml:
<Page x:Class="App8.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App8" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Page.Resources> <SolidColorBrush x:Name="YourColorFromWebService" Color="Red"/><!--let default color be red--> </Page.Resources> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel> <ComboBox SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"> <ComboBoxItem Content="Color №1"/> <ComboBoxItem Content="Color №2"/> <ComboBoxItem Content="Color №3"/> </ComboBox> <TextBlock Text="Text1111111" Foreground="{StaticResource YourColorFromWebService}"/> <TextBlock Text="Text22222" Foreground="{StaticResource YourColorFromWebService}"/> <TextBlock Text="Text33333" Foreground="{StaticResource YourColorFromWebService}"/> </StackPanel> </Grid> </Page>
code behind:
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selected = e.AddedItems.FirstOrDefault() as ComboBoxItem; if (selected != null) { Color color; var name = (string)selected.Content;//or use tags, or index etc for check what color you need to change switch (name) { case "Color №1": color = new Color//insert your color here { A = 255, B = 144, R = 44, G = 29 }; break; case "Color №2": color = new Color { A = 255, B = 222, R = 100, G = 77 }; break; case "Color №3": color = new Color { A = 255, B = 222, R = 100, G = 77 }; break; default: color = Colors.Black; break; } if (this.YourColorFromWebService != null) { this.YourColorFromWebService.Color = color; } } }
- Edited by Oleg Kurzov Thursday, August 29, 2013 7:17 AM
- Marked as answer by 19karabo91 Thursday, August 29, 2013 7:21 AM
Thursday, August 29, 2013 7:16 AM -
Hi,
If you are not using any xaml resource, you should do something mannually, while the OnItemsChanged event fires, you will need to apply the color to all your text on page.
For instance your color is Black, the hex code is #FF000000, note that the Color is built by ARGB, and you have to use a Brush to apply your colors.
Color a = new Color(); a.A = 255;//FF a.B = 0;//00 a.R = 0;//00 a.G = 0;//00 txtbox.Foreground = new SolidColorBrush(a);
You could ref to here to know how to tranlate hex to color: http://stackoverflow.com/questions/16733182/how-to-convert-hexdecimal-code-to-color-in-windows-8
Hope this is what you need,
--James
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Marked as answer by 19karabo91 Thursday, August 29, 2013 6:43 AM
Thursday, August 29, 2013 5:58 AMModerator
All replies
-
- Proposed as answer by Jamles HezModerator Wednesday, August 28, 2013 8:46 AM
Tuesday, August 27, 2013 7:38 AM -
Thanks for the reply but I'm getting my hex string colors from a web service i am not using xaml resource.So how will i manage to do it in this manner?
19karabo91
Thursday, August 29, 2013 5:10 AM -
Hi,
If you are not using any xaml resource, you should do something mannually, while the OnItemsChanged event fires, you will need to apply the color to all your text on page.
For instance your color is Black, the hex code is #FF000000, note that the Color is built by ARGB, and you have to use a Brush to apply your colors.
Color a = new Color(); a.A = 255;//FF a.B = 0;//00 a.R = 0;//00 a.G = 0;//00 txtbox.Foreground = new SolidColorBrush(a);
You could ref to here to know how to tranlate hex to color: http://stackoverflow.com/questions/16733182/how-to-convert-hexdecimal-code-to-color-in-windows-8
Hope this is what you need,
--James
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Marked as answer by 19karabo91 Thursday, August 29, 2013 6:43 AM
Thursday, August 29, 2013 5:58 AMModerator -
Thanks a lot James this is what i needed ,my last question is how do i bind all the text in my app to use that one color ?
19karabo91
Thursday, August 29, 2013 6:43 AM -
xaml:
<Page x:Class="App8.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App8" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Page.Resources> <SolidColorBrush x:Name="YourColorFromWebService" Color="Red"/><!--let default color be red--> </Page.Resources> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel> <ComboBox SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"> <ComboBoxItem Content="Color №1"/> <ComboBoxItem Content="Color №2"/> <ComboBoxItem Content="Color №3"/> </ComboBox> <TextBlock Text="Text1111111" Foreground="{StaticResource YourColorFromWebService}"/> <TextBlock Text="Text22222" Foreground="{StaticResource YourColorFromWebService}"/> <TextBlock Text="Text33333" Foreground="{StaticResource YourColorFromWebService}"/> </StackPanel> </Grid> </Page>
code behind:
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selected = e.AddedItems.FirstOrDefault() as ComboBoxItem; if (selected != null) { Color color; var name = (string)selected.Content;//or use tags, or index etc for check what color you need to change switch (name) { case "Color №1": color = new Color//insert your color here { A = 255, B = 144, R = 44, G = 29 }; break; case "Color №2": color = new Color { A = 255, B = 222, R = 100, G = 77 }; break; case "Color №3": color = new Color { A = 255, B = 222, R = 100, G = 77 }; break; default: color = Colors.Black; break; } if (this.YourColorFromWebService != null) { this.YourColorFromWebService.Color = color; } } }
- Edited by Oleg Kurzov Thursday, August 29, 2013 7:17 AM
- Marked as answer by 19karabo91 Thursday, August 29, 2013 7:21 AM
Thursday, August 29, 2013 7:16 AM -
Thank you so much ,all my work is done all thanks to you .
19karabo91
Thursday, August 29, 2013 7:22 AM