0

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

<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>
4

1 回答 1

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}">
于 2012-06-19T22:00:54.760 回答