网上很多的动态换肤的文章,我这记录下自己开发一个控件时候,写的换肤程序.

 首先,写一个资源文件(xaml)

<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tsc="clr-namespace:BOCO.IMP.Gui.TopoSubject.Controls"
    >
 
    <!--NodeUI MSC Template-->
    <DataTemplate x:Key="NodeDataTemplate_MSC">
      <StackPanel Cursor="Hand" Orientation="Vertical" Background="Transparent">
        <Image Grid.Row="0" Height="30" Grid.Column="0" Source="..\Images\MSC.ico"
           HorizontalAlignment="Center" VerticalAlignment="Top"/>
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Tag}"
                   VerticalAlignment="Center" HorizontalAlignment="Center"
                   FontFamily="宋体" FontSize="10" ClipToBounds="False">
          <TextBlock.Foreground>
            <SolidColorBrush Color="{Binding Path=Tag.Severity,Mode=OneWay,Converter={x:Static tsc:SeverityColorConverter.Default}}"/>
          </TextBlock.Foreground>
        </TextBlock>
      </StackPanel>
    </DataTemplate>

    <!--NodeUI BSC Template-->
    <DataTemplate x:Key="NodeDataTemplate_BSC">
      <StackPanel Cursor="Hand" Orientation="Vertical" Background="Transparent">
        <Image Grid.Row="0"  Height="30" Source="..\Images\TMSC.ico"
           HorizontalAlignment="Center" VerticalAlignment="Top"/>
        <TextBlock Grid.Row="1" Text="{Binding Path=Tag}"
                   VerticalAlignment="Center" HorizontalAlignment="Center"
                   FontFamily="宋体" FontSize="10" ClipToBounds="False">
          <TextBlock.Foreground>
            <SolidColorBrush Color="{Binding Path=Tag.Severity,Mode=OneWay,Converter={x:Static tsc:SeverityColorConverter.Default}}"/>
          </TextBlock.Foreground>
        </TextBlock>
      </StackPanel>
    </DataTemplate>

    <!--NodeUI Default Template-->
    <DataTemplate x:Key="NodeUITemplate">
      <StackPanel Cursor="Hand" Orientation="Vertical" Background="Transparent">
        <Image Grid.Row="0"  Height="30" Source="..\Images\GMSC.ico"
            HorizontalAlignment="Center" VerticalAlignment="Top"/>
        <TextBlock Grid.Row="1" Text="{Binding Path=Tag}"
                   VerticalAlignment="Center" HorizontalAlignment="Center"
                   FontFamily="宋体" FontSize="10" ClipToBounds="False">
          <TextBlock.Foreground>
            <SolidColorBrush Color="{Binding Path=Tag.Severity,Mode=OneWay,Converter={x:Static tsc:SeverityColorConverter.Default}}"/>
        
          </TextBlock.Foreground>
        </TextBlock>
      </StackPanel>
    </DataTemplate>
   
</ResourceDictionary>

然后写个DataTemplateSelector.

  public class NodeTemplateSelector : DataTemplateSelector
    {
        private static NodeTemplateSelector instance;
        public static NodeTemplateSelector Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new NodeTemplateSelector();
                    Uri tempUri = new Uri("/BOCO.IMP.Gui.TopoSubject.Controls;component/Resources/ControlTemplate/ElementTemplates.xaml", UriKind.RelativeOrAbsolute);
                    elementTemplatesResource = Application.LoadComponent(tempUri) as ResourceDictionary;
                }
                return instance;
            }
        }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item != null && item is Node)
            {
                Node node = (Node)item;

                string nodeType = ((NeWrapper)node.Tag).NeType.ToString();

                DataTemplate datatemplate;
                if (!dictionaryTemplate.TryGetValue(nodeType, out datatemplate))
                {
                  datatemplate = elementTemplatesResource["NodeDataTemplate_" + nodeType.ToString().Trim()]  as DataTemplate;
                  if (datatemplate == null)
                      datatemplate = elementTemplatesResource["NodeUITemplate"] as DataTemplate;
                  dictionaryTemplate[nodeType] = datatemplate;
                }
                return datatemplate;
            }
            return null;
        }
        static ResourceDictionary elementTemplatesResource;
        static Dictionary<string, DataTemplate> dictionaryTemplate = new Dictionary<string, DataTemplate>();


    }

 

使用的时候: <ControlTemplate TargetType="{x:Type ovt:NodeUI}">
              <ContentPresenter Content="{Binding}"
                                ContentTemplateSelector="{x:Static tsc:NodeTemplateSelector.Instance}" />
            </ControlTemplate>

posted on 2007-12-14 15:03  颜昌钢  阅读(684)  评论(3编辑  收藏  举报