IsEnabled时更改按钮图像

时间:2011-10-17 16:27:46

标签: wpf image button isenabled

我想在按钮IsEnabled == False时更改按钮图像。

以下是我的示例,绑定很好,当我为False/True更改它时,它仍然无效。

<Button x:Name="btnBackward" Grid.Column="0" Grid.Row="2" Command="{Binding UserWorkflowManager.NavigateBackward}" IsEnabled="{Binding UserWorkflowManager.NavigateBackwardEnable}" Grid.RowSpan="2">
    <Button.Template>
        <ControlTemplate>
            <Image Name="_image" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
                <Image.Style>
                    <Style TargetType="Image">
                        <Setter Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_norm.bmp" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=btnBackward, Path=IsEnabled}" Value="False">
                                    <Setter Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_dis.bmp"/>
                                </DataTrigger>
                            </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </ControlTemplate>
    </Button.Template>
</Button>

3 个答案:

答案 0 :(得分:7)

尝试以下

<Style.Triggers>
    <DataTrigger Binding="{Binding IsEnabled,RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
        <Setter Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_dis.bmp"/>
    </DataTrigger>
</Style.Triggers>

或直接在控件模板中添加触发器,如此

            <ControlTemplate>
                <Image Name="_image" HorizontalAlignment="Center">
                 ..........
                </Image>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="_image" Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_dis.bmp" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

答案 1 :(得分:2)

试试这个:

<Button x:Name="btnBackward" Grid.Column="0" Click="btnBackward_Click">
        <Button.Template>
            <ControlTemplate>
                <Image Name="_image" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Setter Property="Source" Value="/Images/0.png" />
                        </Style>
                    </Image.Style>
                </Image>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Source" Value="/Images/1.png" TargetName="_image"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

        </Button.Template>
    </Button>

您应该在“ControlTemplate.Triggers”中为setter指定“TargetName”属性

答案 2 :(得分:0)

原则是在RelativeSource={RelativeSource Self}}中使用DataTrigger

<DataTrigger
  Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}}"
  Value="False">

实施例

添加此样式:

<Style x:Key="MainButtonStyle" TargetType="Button" BasedOn="{StaticResource ChromelessButtonStyle}">
    <Setter Property="Foreground" Value="Black" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsEnabled,  RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Background" Value="Green" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsEnabled,  RelativeSource={RelativeSource Self}}" Value="False">
            <Setter Property="Background" Value="Blue" />
        </DataTrigger>
    </Style.Triggers>
</Style>

然后将此样式应用于按钮:

<Button x:Name="ActionButton" Style="{StaticResource MainButtonStyle}" Command="{Binding MyCmd}" IsEnabled="{Binding ActionButtonEnabled}"/>