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

时间:2015-10-05 09:52:10

标签: c# wpf

我有一个这样的按钮:

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

然后在代码背后的某个时刻,我在本地将其图像源更改为其他类似的东西:

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

完成此操作后,我将鼠标丢失在&#39;效果可能是因为我覆盖了原始按钮的模板。如何在本地再次告诉&#39;鼠标悬停时触发的按钮?或者换句话说,如何用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>

1 个答案:

答案 0 :(得分:1)

可能是因为属性值优先。我在您的模板中添加了一些资源。现在,当您想要更改某些源值时,请通过访问资源来执行此操作。

另请注意,对于绑定到我们的资源,我们使用的是DynamicResource,否则任何代码级别的更改都将不可见。

以下更改工作轻而易举。

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

然后在代码中,

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