1、問題引出
在我們開發的過程中,有時需要對控件字體進行放大,如果是一個TextBox,大家都很熟練,不就是設置FontSize嗎,這個簡單,那對於DatePicker控件呢?
2、不完美解決方案
很多人又會說不是一樣的道理嗎,你看分分鍾就搞定
但是不懂大家有沒有發現,放大的僅僅只是文本值,下面的彈出框的字體和右側日歷圖標都是沒有變大的,這樣給用戶的是一種很別扭的感覺。
3、完美的解決方案
既然這種方案有問題,那有其它的解決方案嗎?答案肯定是有的。
答案就在微軟的官方文檔:自定義新的WPF Calendar控件
文中介紹了兩種解決方案
方案1:使用LayoutTransform屬性,效果如下
方案2:使用ViewBox,效果如下:
示例代碼

<Window x:Class="WpfApp3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp3" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <DatePicker Width="200" HorizontalAlignment="Left" FontSize="30" VerticalAlignment="Top" Margin="10"/> <Viewbox Visibility="Visible" Stretch="Uniform" Width="300" Height="42" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,90,0,0"> <DatePicker Width="150"/> </Viewbox> <DatePicker Visibility="Hidden" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,170,0,0"> <DatePicker.LayoutTransform> <ScaleTransform ScaleX="1.7" ScaleY="1.7"/> </DatePicker.LayoutTransform> </DatePicker> </Grid> </Window>