close

Markup Extensions and WPF XAML


新增一個類別 SaySomething.cs

image

定義兩個靜態屬性English和Chinese

   1: namespace WpfApp7
   2: {
   3:     public class SaySomething
   4:     {
   5:         public static string English
   6:         {
   7:             get { return "Hello, how are you today?"; }
   8:         }
   9:         public static string Chinese
  10:         {
  11:             get { return "哈囉!今天好嗎?"; }
  12:         }
  13:     }
  14: }


在 MainWindow.xaml

1. 插入兩組TextBlock,使用UniformGrid自動等分畫面

2. TextBlock的Text屬性,以大括號{} 表示標記延伸(markup extension),而且在{ }內的內容會另外另外處理

3. 第一次需先編譯才能在設計畫面中預覽UI結果如下

   1: <Grid>
   2:     <UniformGrid Columns="1">
   3:         <TextBlock Text="{x:Static local:SaySomething.English}"
   4:                Foreground="Red"
   5:                FontSize="40"
   6:                HorizontalAlignment="Center"
   7:                VerticalAlignment="Center"
   8:                />
   9:         <TextBlock Text="{x:Static local:SaySomething.Chinese}"
  10:                Foreground="Blue"
  11:                FontSize="40"
  12:                HorizontalAlignment="Center"
  13:                VerticalAlignment="Center"
  14:                />
  15:     </UniformGrid>
  16:  
  17: </Grid>

image


嘗試{ }回傳double型態的值失敗,找到下面這篇文章應該可以解決

WPF Tutorial - TypeConverter & Markup Extension

arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()