WPF -- 一種添加靜態資源的方式

本文介紹使用獨立的xaml文件添加靜態資源的方式。code

步驟
  1. 建立XAML文件,如ImageButton.xaml,添加ResourceDictionary標籤,並添加靜態資源;
  2. 在App.xaml的Application.Resources標籤中添加xaml資源文件;
  3. 在xaml界面文件中使用StaticResource使用靜態資源。
示例
// ImageButton.xaml
<ResourceDictionary xmlns...>
    <Style x:Key="CustomImageButton" TargetType="Button">
        ...
    </Style>
</ResourceDictionary>

// App.xaml
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/WpfApplication1;component/CustomControls/ImageButton.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

// 使用ImageButton的xaml
<StackPanel>
    <Button Width="50" Height="30" Content="Button" FontSize="14" Style="{StaticResource CustomImageButton}" />
</StackPanel>
相關文章
相關標籤/搜索