WPF多个Resource的调用

1. 定义需要的resource文件

1.1 从网上down的一个滑块checkBox

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="CheckRadioFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="SliderCheckBox" TargetType="{x:Type CheckBox}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="StoryboardIsChecked">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="CheckFlag">
                                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="14"/>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="StoryboardIsCheckedOff">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="CheckFlag">
                                <EasingDoubleKeyFrame KeyTime="0" Value="14"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <BulletDecorator Background="Transparent" SnapsToDevicePixels="true">
                        <BulletDecorator.Bullet>
                            <Border x:Name="ForegroundPanel" BorderThickness="1" Width="35" Height="20" CornerRadius="10">
                                <Canvas>
                                    <Border Background="White" x:Name="CheckFlag" CornerRadius="10" VerticalAlignment="Center" BorderThickness="1" Width="19" Height="18" RenderTransformOrigin="0.5,0.5">
                                        <Border.RenderTransform>
                                            <TransformGroup>
                                                <ScaleTransform/>
                                                <SkewTransform/>
                                                <RotateTransform/>
                                                <TranslateTransform/>
                                            </TransformGroup>
                                        </Border.RenderTransform>
                                        <Border.Effect>
                                            <DropShadowEffect ShadowDepth="1" Direction="180" />
                                        </Border.Effect>
                                    </Border>
                                </Canvas>
                            </Border>
                        </BulletDecorator.Bullet>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                    </BulletDecorator>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasContent" Value="true">
                            <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
                            <Setter Property="Padding" Value="4,0,0,0"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter TargetName="ForegroundPanel" Property="Background" Value="DodgerBlue" />
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="BeginStoryboardCheckedTrue" Storyboard="{StaticResource StoryboardIsChecked}" />
                                <RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedFalse" />
                            </Trigger.EnterActions>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter TargetName="ForegroundPanel" Property="Background" Value="Gray" />
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="BeginStoryboardCheckedFalse" Storyboard="{StaticResource StoryboardIsCheckedOff}" />
                                <RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedTrue" />
                            </Trigger.EnterActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

1.2 用于绑定对象的listbox的DataTemplate

<ResourceDictionary x:Class="Alf7.WPF_Monitor.Styles.ListBoxItemTempalte"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DataTemplate x:Key="ControlTemplate">        
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="3,5,0,5">
            <TextBlock FontFamily="Arial" Text="{Binding Path=type, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="DodgerBlue"  Width="80" />
            <TextBlock FontFamily="Arial" Text="{Binding Path=name, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center"   Width="130" Margin="0,0,5,0"  ToolTip="{Binding Path=friendlyName, UpdateSourceTrigger=PropertyChanged}"/>
            <CheckBox FontFamily="Arial"  Style="{DynamicResource SliderCheckBox}"   Content="{Binding Path=state, UpdateSourceTrigger=PropertyChanged}" Checked="onChecked" IsChecked="{Binding Path=isStoped, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Unchecked="onUnchecked" VerticalAlignment="Center"/>
        </StackPanel>
    </DataTemplate>
    <Style TargetType="ListBoxItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        </Style.Resources>
    </Style>
</ResourceDictionary>

 

2. 调用时利用ResourceDictionary.MergedDictionaries

                <Grid.Resources>
                    <ResourceDictionary>
                        <ResourceDictionary.MergedDictionaries>
                            <ResourceDictionary Source="Styles\SliderCheckBox.xaml" />
                            <ResourceDictionary Source="Styles\ListBoxItemTempalte.xaml" />
                        </ResourceDictionary.MergedDictionaries>
                    </ResourceDictionary>
                </Grid.Resources>

在需要用到resource的时候还是按照之前的方式。

ItemTemplate="{StaticResource ControlTemplate}" 

 

posted @ 2014-03-24 17:24  Alf7  阅读(1121)  评论(0编辑  收藏  举报