首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WPF 資料模板與樣式結合:資料視覺化與屬性集中管理

     編輯:關於.NET

資料模板:為非視覺物件定義外觀

當按鈕內容為自訂類別 PlayerMove(含 PlayerName、MoveNumber)時,預設僅顯示型別名稱。透過 DataTemplate 指定 DataType,WPF 遇到該型別自動展開模板。

<DataTemplate DataType="{x:Type local:PlayerMove}">
  <Grid>
    <TextBlock Text="{Binding PlayerName}" FontSize="32" FontWeight="Bold" .../>
    <TextBlock Text="{Binding MoveNumber}" FontSize="16" FontStyle="Italic" .../>
  </Grid>
</DataTemplate>
無模板顯示型別名
套用資料模板後

樣式抽離屬性設定

將模板內重複屬性(字體、對齊等)移至具名樣式,再由 TextBlock.Style="{StaticResource CellTextStyle}" 引用。利於維護、支援換膚/主題(第 6 章詳述)。

<Style x:Key="CellTextStyle" TargetType="{x:Type TextBlock}">
  <Setter Property="FontSize" Value="32"/> ...
</Style>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved