如何在WPF中基于资源字典创建资源

时间:2018-02-08 09:31:59

标签: wpf xaml

我创建了一个接收2个参数的资源字典:releasedImage和PressedImage:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:SwitchesLibrary">
<Style TargetType="local:ImageButton" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Template">
            <Setter.Value>
            <ControlTemplate TargetType="local:ImageButton">
                <Grid>
                    <Image x:Name="PressedButton"
                 Source="{TemplateBinding PressedImage}" />
                    <Image x:Name="ReleasedButton"
                 Source="{TemplateBinding ReleasedImage}" />
                </Grid>
            </Setter.Value>
        </Setter>
    </Style>

在另一个lib中,我会使用几个带有相同图像的按钮。所以我想在这个lib中创建一个具有特定PressedImageReleasedImage的资源, 像这样:

<UserControl x:Class="ExamplePanelLibrary.ExamplePanelControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ExamplePanelLibrary"
             xmlns:SwitchesLibrary="clr-namespace:SwitchesLibrary;assembly=SwitchesLibrary"
             mc:Ignorable="d" 
             d:DesignHeight="760" d:DesignWidth="754">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary 
                Source="pack://application:,,,/SwitchesLibrary;component/ImageButtonStyle.xaml">
                </ResourceDictionary>              
            </ResourceDictionary.MergedDictionaries>
            <ImageBrush x:Key="ButtonPressed" ImageSource="Images/PushButtons/OSB_Pushed.png"/>
            <ImageBrush x:Key="ButtonReleased" ImageSource="Images/PushButtons/OSB_Released.png"/>
            <Style 
                x:Key="OSBPushButton" 
                TargetType="SwitchesLibrary:ImageButton" 
                ReleasedImage="Images/SpecificButtonReleased.png"
                PressedImage="Images/SpecificButtonPressed.png"
             />
        </ResourceDictionary>

我们可以这样做吗?

2 个答案:

答案 0 :(得分:1)

这可能有效:

<Style x:Key="OSBPushButton" 
       TargetType="SwitchesLibrary:SpecificImageButton"
       BasedOn="{StaticResource {x:Type local:ImageButton}}">

    <Setter Property="ReleasedImage" Value="Images/SpecificButtonReleased.png"/>
    <Setter Property="PressedImage" Value="Images/SpecificButtonPressed.png"/> 
</Style>

答案 1 :(得分:0)

克莱门斯给出了正确的答案。我只想清楚地重写一下:

            <Style
            x:Key="OSBPushButton" 
            TargetType="SwitchesLibrary:ImageButton"
            BasedOn="{StaticResource {x:Type SwitchesLibrary:ImageButton}}">

            <Setter Property="ReleasedImage" Value="Images/SpecificButtonReleased.png"/>
            <Setter Property="PressedImage" Value="Images/SpecificButtonPressed.png"/>
        </Style>

并像这样使用它:

 <SwitchesLibrary:ImageButton x:Name="OSB_1" Style="{StaticResource OSBPushButton}"/>