网格参考图像样式和按钮样式

时间:2012-06-19 17:10:27

标签: c# wpf xaml reference styles

我目前在网格中有几个具有相同xaml图像样式和按钮样式的按钮。我正在尝试从按钮的样式功能调用网格引用。我可以做按钮样式工作,但我遇到了编码图像样式的问题,因此它是一个调用(xaml的新功能)。感谢您的帮助。

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

<Button.Style>
    <Style TargetType="Button">
        <Setter Property="IsEnabled"
                Value="False" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding CanEdit}"
                         Value="True">
                <Setter Property="IsEnabled"
                        Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Button.Style>

1 个答案:

答案 0 :(得分:1)

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

<!--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}">