• 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>
    
  • 相关阅读:
    Java学习10.22(Javaweb对输入信息进行验证——常用的方法)
    mysql with python
    Linux
    Python 基础的一些习题
    Python 推导式、迭代器、生成器、模块和包
    Python 文件操作、异常
    Python 部分内置函数、作用域、闭包、递归
    Python 基础函数、解包
    Python 条件与循环
    Python 集合、字典、运算符
  • 原文地址:https://www.cnblogs.com/sclu/p/13557914.html
Copyright © 2020-2023  润新知