1 回答

TA貢獻1801條經(jīng)驗 獲得超8個贊
有一個簡單的示例,說明如何在不同的視圖/窗口之間共享 XAML 代碼。創(chuàng)建一個ResourceDictionary,定義共享屬性/樣式/控件模板,如下所示
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? ? ? ? ? ? ? ? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
? ? <Style TargetType="{x:Type Label}">
? ? ? ? <Setter Property="Foreground" Value="Black" />
? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />
? ? </Style>
? ? <Style TargetType="{x:Type Button}">
? ? ? ? <Setter Property="Foreground" Value="Black" />
? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />
? ? </Style>
? ? <Style TargetType="{x:Type TextBox}">
? ? ? ? <Setter Property="Foreground" Value="Black" />
? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />
? ? </Style>
? ? <Style TargetType="{x:Type ScrollBar}">
? ? ? ? <Setter Property="Foreground" Value="Black" />
? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />
? ? </Style>
? ? <Style TargetType="Label" x:Key="TitleStyle" BasedOn="{StaticResource {x:Type Label}}">
? ? ? ? <Setter Property="HorizontalContentAlignment" Value="Center"/>
? ? ? ? <Setter Property="VerticalContentAlignment" Value="Center" />
? ? ? ? <Setter Property="HorizontalAlignment" Value="Stretch" />
? ? ? ? <Setter Property="VerticalAlignment" Value="Stretch"/>
? ? ? ? <Setter Property="FontSize" Value="16" />
? ? </Style>
</ResourceDictionary>
您可以將此字典添加到應用程序/窗口MergedDictionaries來使用它們,例如
<Window.Resources>
? ? <ResourceDictionary>
? ? ? ? <ResourceDictionary.MergedDictionaries>
? ? ? ? ? ? <ResourceDictionary Source="Styles.xaml"/>
? ? ? ? </ResourceDictionary.MergedDictionaries>
? ? </ResourceDictionary>
</Window.Resources>
請注意,這只是一個簡單的例子來簡要解釋這個想法。您還可以查看Style.TargetType
文檔以查看樣式之間TargetType
和x:Key
樣式中的解釋
- 1 回答
- 0 關(guān)注
- 137 瀏覽
添加回答
舉報