IsEnabled 时更改按钮图像

发布于 2024-12-10 18:53:09 字数 1345 浏览 0 评论 0原文

我想在按钮 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>

I want to change button image when button IsEnabled == False.

Below is my example, bindings are fine, when I change them for False/True it is still not working.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长发绾君心 2024-12-17 18:53:09

尝试以下操作

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

Try the following

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

Or add the trigger directly in the control template like so

            <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>
一念一轮回 2024-12-17 18:53:09

试试这个:

<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”中的设置器指定“TargetName”属性

Try this:

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

You should specify "TargetName" property for a setter within "ControlTemplate.Triggers"

救赎№ 2024-12-17 18:53:09

解决办法是在 DataTrigger 中使用 RelativeSource={RelativeSource Self}}

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

The soution is is to use RelativeSource={RelativeSource Self}} in your DataTrigger.

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

Example

Add this style:

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

Then apply this style to the button:

<Button x:Name="ActionButton" Style="{StaticResource MainButtonStyle}" Command="{Binding MyCmd}" IsEnabled="{Binding ActionButtonEnabled}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文