开发者

How to use a custom style for different controls in XAML

I have coded a custom style for a button in a ressource-dic. My question is, is it possible to use this style for different buttons? Means that I need to set params by calling the style to switch die picture target. (How?)

Window:

<Button .... Style="{DynamicResource downloadButtonStyle}" IsEnabled="True"  />

RessourceDic:

<Style x:Key="downloadButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="IsEnabled" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image x:Name="PART_img" Source="/FtpUploadClient;component/media/box_48.png"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="PART_img" Property="Source" Value="/FtpUploadClient;component/media/box_download_48.png" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="PART_img" Property="Source" Value="/FtpUploadClient;component/media/box_deactivated_48.png"开发者_如何学Go />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


You can use Attached Properties. This will enable you to specify the source for the Images inside the Template for each Button instance

<Button Style="{DynamicResource downloadButtonStyle}" 
        ex:ButtonExtension.DefaultImageSource="DefaultImageSource"
        ex:ButtonExtension.MouseOverImageSource="MouseOverImageSource"
        ex:ButtonExtension.DisabledImageSource="DisabledImageSource" /> 

In the Template

<ControlTemplate>
    <Image x:Name="PART_img" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                              Path=(ex:ButtonExtension.DefaultImageSource)}"/
    <ControlTemplate.Triggers>     
        <Trigger Property="IsMouseOver" Value="True">     
            <Setter TargetName="PART_img" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                          Path=(ex:ButtonExtension.MouseOverImageSource)}" />
        </Trigger>     
        <Trigger Property="IsEnabled" Value="False">     
            <Setter TargetName="PART_img" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                          Path=(ex:ButtonExtension.DisabledImageSource)}" />     
        </Trigger>     
    </ControlTemplate.Triggers>     
</ControlTemplate>

ButtonExtension

public class ButtonExtension
{
    public static DependencyProperty DefaultImageSourceProperty =
        DependencyProperty.RegisterAttached("DefaultImageSource",
                                            typeof(ImageSource),
                                            typeof(ButtonExtension),
                                            new PropertyMetadata(null));
    public static ImageSource GetImageSource(DependencyObject target)
    {
        return (ImageSource)target.GetValue(DefaultImageSourceProperty);
    }
    public static void SetImageSource(DependencyObject target, ImageSource value)
    {
        target.SetValue(DefaultImageSourceProperty, value);
    }

    // Repeat for MouseOverImageSource and DisabledImageSource
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜