zoukankan      html  css  js  c++  java
  • Silverlight的皮肤转换和datagrid数据显示

    1 文件安排

     

    2 引用添加

    3 MainPage.xaml文件

    <UserControl
        
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="datagrid.MainPage"
        xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable
    ="d" d:DesignWidth="640" d:DesignHeight="480">
        
    <Grid x:Name="LayoutRoot">
            
    <Grid.RowDefinitions>
                
    <RowDefinition Height="Auto"/>
                
    <RowDefinition Height="Auto"/>
            
    </Grid.RowDefinitions>
            
    <ComboBox Name="selecttheme" Width="200"  SelectionChanged="ComboBox_SelectionChanged" >
            
    </ComboBox>
            
    <data:DataGrid x:Name="datagrid" AutoGenerateColumns="True" Grid.Row="1"/>
        
    </Grid>
    </UserControl>

    4 MainPage.xaml.cs文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;


    using System.Windows.Controls.Theming;

    namespace datagrid
    {
        
    public partial class MainPage : UserControl
        {
            
    public MainPage()
            {
                InitializeComponent();
                selecttheme.Items.Add(
    new ComboBoxItem() { Name = "ExpressionDark", Content = "ExpressionDark", DataContext = "themes/ExpressionDark.xaml", IsEnabled = true,IsSelected=true });
                selecttheme.Items.Add((
    new ComboBoxItem() { Name = "BubbleCreme", Content = "BubbleCreme", DataContext = "themes/BubbleCreme.xaml", IsEnabled = true }));
                selecttheme.Items.Add((
    new ComboBoxItem() { Name = "ShinyBlue", Content = "ShinyBlue", DataContext = "themes/ShinyBlue.xaml", IsEnabled = true }));
                selecttheme.Items.Add((
    new ComboBoxItem() { Name = "ShinyRed", Content = "ShinyRed", DataContext = "themes/ShinyRed.xaml", IsEnabled = true }));
                datagrid.ItemsSource 
    = Photo.GetPersonList();
            }

            
    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                SetTheme(selecttheme.SelectedItem 
    as ComboBoxItem);
            }

            
    //设置相应的theme
            void SetTheme(ComboBoxItem comboBoxItem)
            {
                
    if (comboBoxItem != null)
                {
                    Uri uri 
    = new Uri(comboBoxItem.DataContext.ToString(), UriKind.Relative);
                    ImplicitStyleManager.SetResourceDictionaryUri(LayoutRoot, uri);
                    ImplicitStyleManager.SetApplyMode(LayoutRoot, ImplicitStylesApplyMode.Auto);
                    ImplicitStyleManager.Apply(LayoutRoot);
                    datagrid.ItemsSource 
    = Photo.GetPersonList();
                }
            }
        }

        
    public class Photo
        {
            
    public int ID { getset; }
            
    public int Width { getset; }
            
    public int Height { getset; }
            
    public bool IsColorful { getset; }
            
    public DateTime CreateDate { getset; }

            
    public Photo(int id, int width, int height, bool isColorful, DateTime createDate)
            {
                
    this.ID = id;
                
    this.Width = width;
                
    this.Height = height;
                
    this.IsColorful = isColorful;
                
    this.CreateDate = createDate;
            }

            
    public static List<Photo> GetPersonList()
            {
                List
    <Photo> phtoList = new List<Photo>();
                Photo p1 
    = new Photo(10011024768truenew DateTime(200888));
                Photo p2 
    = new Photo(1002800600truenew DateTime(200889));
                Photo p3 
    = new Photo(100312801024falsenew DateTime(2008810));
                Photo p4 
    = new Photo(1004320240falsenew DateTime(2008811));
                Photo p5 
    = new Photo(1005800600truenew DateTime(2008813));
                Photo p6 
    = new Photo(1006600600falsenew DateTime(2008814));
                phtoList.Add(p1);
                phtoList.Add(p2);
                phtoList.Add(p3);
                phtoList.Add(p4);
                phtoList.Add(p5);
                phtoList.Add(p6);
                
    return phtoList;
            }
        }
    }

    5.运行效果:

    6 总结

    6.1 隐式样式管理器ImplicitStyleManager

    该类的使用方法,是在某个根元素上设置一个附加属性(Attached Property),然后,该元素下属的视觉树里符合特定类型的子元素的样式,就可以被自动应用隐式样式了。

    ApplyMode有三个可选值: None, OneTime和Auto
    None: 不使用样式
    OneTime: 在页面加载后接受一次指定的样式
    Auto: 在运行时动态添加的控件也将会使用指定样式
    因此Border在加载时会使用一次引入的主题样式

  • 相关阅读:
    技嘉H81M-DS2 主板安装 XP方法,及网卡驱动安装
    XCode5无法设置Deployment Target的解决办法
    CALayer的contentsRect
    有道词典for mac不能取词解决方案
    应用沙盒(Application Sandbox)
    模态视图控制对象
    UUID(即GUID)
    UIView的endEditing:方法
    让UITableView进入编辑模式
    presentModalViewController方法,present一个透明的viewController,带动画效果
  • 原文地址:https://www.cnblogs.com/yongfeng/p/1748688.html
Copyright © 2011-2022 走看看