调用图像样式

时间:2012-06-19 19:37:02

标签: c# wpf xaml reference styles

有没有办法创建一个可调用的图像样式?我在用户控件上有多个按钮,它们具有相同的按钮和按钮图像样式。我可以设置按钮样式,以便可以从按钮的样式(动态资源)调用它。 以下是我的代码和图像样式代码的示例:

<Grid.Resources>
    <Style TargetType="Button"
           x:Key="ButtonEditSaveStyle">
        <Setter Property="IsEnabled"
                Value="False" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding CanEdit}"
                         Value="True">
                <Setter Property="IsEnabled"
                        Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Grid.Resources>

<Button Width="32"
        Height="22"
        HorizontalAlignment="Left"
        VerticalAlignment="Center"
        Name="gdEmployeeInfo_btnUpdateRecord"
        Click="gdEmployeeInfo_btnUpdateRecord_Click" 
        Style="{DynamicResource ResourceKey=ButtonEditSaveStyle}"/>

我想设置我的代码,以便我可以让上面的调用有一个额外的调用或组合调用来设置图像样式。我知道我可以在按钮中显示图像样式(如下一个代码所示),但我希望有一个地方可以使用相同的样式设置更新多个按钮。图片样式代码:

<Image>
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source"
                    Value="edit_32.png" />
            <Setter Property="Stretch"
                    Value="Uniform" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEditing}"
                             Value="True">
                    <Setter Property="Source"
                            Value="save_smallest.png" />
                    <Setter Property="Stretch"
                            Value="Uniform" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

1 个答案:

答案 0 :(得分:0)

找出我的问题的答案。这组代码允许我使用双重设置,同时只为按钮的样式创建一个调用:

<!--SaveEditImageSwitch-->
<Image x:Key="SaveEditImage" x:Shared="False">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source"
                    Value="edit_32.png" />
            <Setter Property="Stretch"
                    Value="Uniform" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEditing}"
                             Value="True">
                    <Setter Property="Source"
                            Value="save_smallest.png" />
                    <Setter Property="Stretch"
                            Value="Uniform" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

<!--ButtonEditSaveStyle-->
<Style TargetType="Button"
       x:Key="ButtonEditSaveStyle">
    <Setter Property="IsEnabled"
            Value="False" />
    <Setter Property="Content"
            Value="{DynamicResource ResourceKey=SaveEditImage}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding CanEdit}"
                     Value="True">
            <Setter Property="IsEnabled"
                    Value="True" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<Button Width="32"
        Height="22"
        HorizontalAlignment="Left"
        VerticalAlignment="Center"
        Name="gdEmployeeInfo_btnUpdateRecord"
        Click="gdEmployeeInfo_btnUpdateRecord_Click" 
        Style="{DynamicResource ResourceKey=ButtonEditSaveStyle}">