Seven Mobile Apps in Seven Weeks (for james shahan) by Tony Hillerson

Seven Mobile Apps in Seven Weeks (for james shahan) by Tony Hillerson

Author:Tony Hillerson
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: The Pragmatic Bookshelf, LLC (600184)


Styling List Items

Let’s look at how we’d like the components in the list item to look. We’ll make the Name bold and the font size on the Symbol smaller and dark grey.

Windows/windows_02_04_styling_the_list_item/Stocks/Stocks/MainPage.xaml

​ <TextBlock Text=​"{Binding Name}"​

​ Grid.Row=​"0"​ Grid.Column=​"0"​ FontWeight=​"Bold"​ />

​ <TextBlock Text=​"{Binding LastTradePriceOnly}"​

​ Grid.Row=​"0"​ Grid.Column=​"1"​ Grid.ColumnSpan=​"2"​

​ HorizontalAlignment=​"Right"​ />

​ <TextBlock Text=​"{Binding Symbol}"​

​ Grid.Row=​"1"​ Grid.Column=​"0"​

​ FontSize=​"10.667"​ VerticalAlignment=​"Center"​

​ Foreground=​"#FF505050"​ />

That looks better, or at least helps to distinguish pieces of information from each other. However, it’d be better if we could define styles in a more reusable way; something like a CSS style sheet. It turns out we can, by defining styles in the Resources section of a context—for instance, the Application.

Defining Style Resources

Let’s put a set of styles inside the Application Resources property. The Resources property of a Context is like a dictionary or a key/value pair.

Windows/windows_02_05_style_resource/Stocks/Stocks/App.xaml

​ <Application.Resources>

​ <Style TargetType=​"TextBlock"​ x:Key=​"StockName"​>

​ <Setter Property=​"FontWeight"​ Value=​"Bold"​ />

​ </Style>

​ <Style TargetType=​"TextBlock"​ x:Key=​"StockSymbol"​>

​ <Setter Property=​"FontSize"​ Value=​"10"​ />

​ <Setter Property=​"Foreground"​ Value=​"#FF505050"​ />

​ </Style>

​ <SolidColorBrush Color=​"Red"​ x:Key=​"DownColorBrush"​ />

​ <SolidColorBrush Color=​"Green"​ x:Key=​"UpColorBrush"​ />

​ </Application.Resources>

Each Style tag has an x:Key property, which we can use to identify it. Also, since we’ve defined the styles in the Application’s resources instead of a certain Page’s, we can refer to them from anywhere. If we’d defined them in a particular page, we’d have access to them from the context of that page only.

Let’s look at how we apply those styles.

Windows/windows_02_05_style_resource/Stocks/Stocks/MainPage.xaml

​ <TextBlock Text=​"{Binding Name}"​ Style=​"{StaticResource StockName}"​

​ Grid.Row=​"0"​ Grid.Column=​"0"​ />

​ <TextBlock Text=​"{Binding LastTradePriceOnly}"​

​ Grid.Row=​"0"​ Grid.Column=​"1"​ Grid.ColumnSpan=​"2"​

​ HorizontalAlignment=​"Right"​ />

​ <TextBlock Text=​"{Binding Symbol}"​ Style=​"{StaticResource StockSymbol}"​

​ Grid.Row=​"1"​ Grid.Column=​"0"​

​ VerticalAlignment=​"Center"​ />

​ <StackPanel Orientation=​"Horizontal"​

​ Grid.Row=​"1"​ Grid.Column=​"1"​ HorizontalAlignment=​"Right"​>

​ <TextBlock Text=​"{Binding Change}"​

​ Foreground=​"{Binding​ ​IsDown,​

​ Converter=​{StaticResource​ ​ColorConverter}}"​/>

​ <TextBlock Text=​"{Binding PercentChange}"​ Margin=​"8,0,0,0"​

​ Foreground=​"{Binding​ ​IsDown,​

​ Converter=​{StaticResource​ ​ColorConverter}}"​/>

​ </StackPanel>

Again, binding syntax comes into play to bind a component’s Style property to a resource key, like the ones we just defined. Here, we’ve bound the Style property of the name and stock symbol to the styles we just created.

Notice also that we’ve used a more complex binding syntax for the Foreground property of the change and percent change fields. We’re using a value converter to apply a conditional style. If the stock is up in value we’ll display the changes in green, and if it’s down we’ll use red instead. Let’s look at how that works.

Conditional Styles

The Foreground property of a TextBlock takes a Brush object, which defines things like the color and border for a visual element. To choose whether to use a red or green color, we can use a feature of binding where we run a bound value through a converter before the binding framework notifies the bound component of its new value. In this particular case, we’ve bound the color of the text block to a Boolean property of the model, IsDown, which is converted to a Brush. We do this by defining an IValueConverter.

Windows/windows_02_05_style_resource/Stocks/Stocks/ValueConverters/IsDownToColorConverter.cs

​ ​namespace​ Stocks.ValueConverters {

​ ​public​ ​class​ IsDownToColorConverter : IValueConverter {

​ ​public​ SolidColorBrush UpColorBrush { ​get​; ​set​; }

​ ​public​ SolidColorBrush DownColorBrush { ​get​; ​set​; }

​ ​public​ ​object​ Convert(​object​ ​value​,

​ Type targetType,

​ ​object​ parameter,

​ ​string​ language) {

​ ​if​ ((​bool​)​value​) { ​// is



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.