wpf 添加资源字典(xaml)

Wpf添加资源字典

1.新建一个字典,设置好名称空间,key,TargetType等。

例如添加一个ListBox的资源字典

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfMyDemo">
    <Style x:Key="ViewInfoStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid Height="35" x:Name="grid">
                        <!--<Border Background="{Binding UserBackground}" Width="40" Height="40" CornerRadius="4" HorizontalAlignment="Left" Margin="5 0 0 0">
                            <TextBlock Text="{Binding Header}" FontSize="23" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Border>
                        <TextBlock Text="{Binding Name}" Margin="55 7 0 0" FontSize="13"/>
                        <TextBlock Text="{Binding Info}" Foreground="#808080" Margin="55 30 0 0"/>
                        <TextBlock Text="{Binding Count,StringFormat={}{0}人}" Foreground="#808080" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0 0 5 0"/>-->
                                                
                        <CheckBox Width="20" IsChecked="{Binding IsSelection}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                        <TextBlock Text="{Binding Side}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15"/>
                        <TextBlock Text="{Binding Count,StringFormat={}{0}Axis}"  VerticalAlignment="Center" FontSize="15" HorizontalAlignment="Right"/>

                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="grid" Property="Background" Value="#fceeb9"/>
                        </Trigger>
                        <Trigger Property="Selector.IsSelected" Value="true">
                            <Setter TargetName="grid" Property="Background" Value="#fae388"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

2.在App.xaml中声明资源字典

<Application x:Class="WpfMyDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfMyDemo"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/WpfMyDemo;component/Styles/TabControl.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/WpfMyDemo;component/Styles/ListBox.xaml"/>

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

3.在MainWindow中添加相应的控件,引用创建的资源字典,根据key

ListBox VerticalAlignment="Top"  BorderThickness="1" ItemContainerStyle="{StaticResource ViewInfoStyle}" x:Name="ViewInfoListBox" BorderBrush="#eaeaeb" 
                                 Background="#FF3A3831" Height="220" Margin="0,30,0,0" >
                                </ListBox>
原文地址:https://www.cnblogs.com/sclu/p/13557914.html