简体   繁体   English

如何以编程方式更改按钮的图像源?

[英]How to change button's image source programmatically?

I have a button like this: 我有一个像这样的按钮:

<Button Name="Btn_Import"
        Grid.Row="33"
        Grid.Column="15"
        Grid.ColumnSpan="36"
        Grid.RowSpan="36"
        Click="Btn_Import_Click"
        MouseEnter="import_desc"
        MouseLeave="desc_clear">
    <Button.Template>
        <ControlTemplate>
            <Grid RenderTransformOrigin="0.5,0.5"
                  x:Name="bg">
                <Image Name="import_image"
                       Source="/Images/01_Main_Screen/MS_START_IMPORT_NORMAL.png" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver"
                         Value="True">
                    <!-- hover effect -->
                    <Setter TargetName="import_image"
                            Property="Source"
                            Value="/Images/01_Main_Screen/MS_START_IMPORT_OVER.png" />
                </Trigger>
                <Trigger Property="Button.IsPressed"
                         Value="True">
                    <!-- press effect -->
                    <Setter TargetName="bg"
                            Property="RenderTransform">
                        <Setter.Value>
                            <ScaleTransform ScaleX="0.9"
                                            ScaleY="0.9" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
    <Button.Style>
        <!-- initially disabled-->
        <Style TargetType="Button">
            <!--<Setter Property="IsEnabled" Value="False" />-->
            <!--<Setter Property="Opacity" Value="0.3" />-->
        </Style>
    </Button.Style>
</Button>

And then at some point in code behind, I locally change its image source to something else like this: 然后,在后面的代码中的某个点上,我在本地将其图像源更改为如下所示:

ControlTemplate ct1 = Btn_Import.Template;
Image btnImage1 = (Image)ct1.FindName("import_image", Btn_Import);
btnImage1.Source = new BitmapImage(new Uri("/Images/01_Main_Screen/MS_START_IMPORT_FINISH.png", UriKind.RelativeOrAbsolute));

After doing this, I lose the 'mouse over' effect probably because I overwrite the original button's template. 完成此操作后,我失去了“鼠标悬停”效果,这可能是因为我覆盖了原始按钮的模板。 How to locally again 'tell' the button to trigger on mouse over? 如何在本地再次“告诉”按钮以在鼠标悬停时触发? Or in other words, how to write this in C#: 换句话说,如何用C#编写:

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" 
          Value="True">
     <!-- hover effect -->
     <Setter TargetName="import_image" Property="Source"
             Value="/Images/01_Main_Screen/MS_START_IMPORT_OVER.png" />
    </Trigger>

It is probably because of property value precedence. 可能是因为属性值优先。 I have added some resources in your template. 我在您的模板中添加了一些资源。 Now when you want to change some source value, do it by accessing the resource. 现在,当您想更改某些源值时,可以通过访问资源来完成。

Also note that for binding to our resources we are using DynamicResource otherwise any code level changes won't be visible. 还要注意,为了绑定到我们的资源,我们使用DynamicResource,否则任何代码级别的更改都将不可见。

Below changes are working like a breeze. 下面的变化像微风一样起作用。

<Button.Template>
                <ControlTemplate>

                    <ControlTemplate.Resources>
                        <Image x:Key="NORMAL" Source="images/01_Main_Screen/MS_START_IMPORT_NORMAL.png"/>
                        <Image x:Key="OVER" Source="images/01_Main_Screen/MS_START_IMPORT_OVER.png"/>
                    </ControlTemplate.Resources>

                    <Grid RenderTransformOrigin="0.5,0.5"
                  x:Name="bg">
                        <Label x:Name="import_image" Content="{DynamicResource NORMAL}" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch"/>                        
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver"
                         Value="True">
                            <!-- hover effect -->
                            <Setter TargetName="import_image"
                            Property="Content"
                            Value="{DynamicResource OVER}" />
                            <Setter TargetName="bg"
                            Property="Background"
                            Value="Purple" />
                        </Trigger>
                        <Trigger Property="Button.IsPressed"
                         Value="True">
                            <!-- press effect -->
                            <Setter TargetName="bg"
                            Property="RenderTransform">
                                <Setter.Value>
                                    <ScaleTransform ScaleX="0.9"
                                            ScaleY="0.9" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>

Then in code, 然后在代码中

ControlTemplate ct1 = Btn_Import.Template;
Image normalImage = (Image)ct1.Resources["NORMAL"];
normalImage.Source = new BitmapImage(new Uri("Images/01_Main_Screen/MS_START_IMPORT_FINISH.png", UriKind.RelativeOrAbsolute));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM