c# - 更改按钮模板内的图像源

标签 c# .net wpf image button

我在 WPF 应用程序中有一个按钮。我正在呈现图像而不是按钮。 当我的应用程序启动时,所有控件都被禁用,直到用户登录。我希望我的应用程序以图像为灰色的按钮启动 ->禁用。当有人登录时,我想更改为彩色图像。但我无法访问按钮模板中的图像。有人可以帮忙吗?谢谢。

我的按钮的代码 -

<Button  Height="55" Background="CornflowerBlue" Foreground="White" FontWeight="Normal" Name="Button_Cancel" FontSize="12" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="55" Click="Button_Cancel_Click">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <StackPanel >
                    <Image Source="Images/myImage.png" Name="Image_Cancel"/>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Button.BitmapEffect" >
                            <Setter.Value>
                                <DropShadowBitmapEffect Color="LightGray" Direction="300" ShadowDepth="5" >
                                </DropShadowBitmapEffect>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Button.RenderTransform">
                            <Setter.Value>
                                <TransformGroup>
                                    <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="0.970" ScaleY="0.970">
                                    </ScaleTransform>
                                    <!--<SkewTransform AngleY="2" CenterY="-100"/>-->
                                </TransformGroup>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>

最佳答案

您可以通过触发器将图像与 IsEnabled 属性 Hook ,然后您可能不需要代码隐藏访问。

<Image>
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="Images/Image_Normal.png" />
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Source" Value="Images/Image_Disabled" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

关于c# - 更改按钮模板内的图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186407/

相关文章:

c# - New Relic .NET 代理如何工作?

WPF 临时显示卡住

c# - kernel.Get<type> 处的 InvalidCastException

c# - 无法使用本地部署的 ASP.NET REST API

c# - parseexact 时,字符串未被识别为有效的 DateTime

java - .NET (Mono) 是否支持跨平台文件操作和跨平台音频元数据处理(通过库)?

c# - UserControl 中的 TextBlock 不显示文本

c# - 对启用分页的 gridview 进行排序

c# - 我怎样才能显示:none element using Selenium Webdriver c#?

c# - 如果抛出异常,.Net Web API 偶尔会完全不返回任何响应