zoukankan      html  css  js  c++  java
  • 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>
    
  • 相关阅读:
    ActionContext和ServletActionContext
    1.有Dao为什么还要Service?
    1.DBCP和CP30的区别
    设计模式之单例模式(java实现)
    DOM之练习2
    js之DOM练习题
    动态改变dom结构常用方法
    js 自动类型转换
    定位属性position
    html基础
  • 原文地址:https://www.cnblogs.com/sclu/p/13557914.html
Copyright © 2011-2022 走看看