1

I'm trying to create a three-state Image Button in run time. There is existing method in XAML but I wonder how to do it in code behind (i.e. how to translate the following code to C#)?

<Button>
   <Button.Template>
      <ControlTemplate TargetType="{x:Type Button}">
         <Grid>
            <Image Name="Normal" Source="Resources/Normal.png"/>
            <Image Name="Pressed" Source="Resources/Pressed.png" Visibility="Hidden"/>
            <Image Name="Disabled" Source="Resources/Disabled.png" Visibility="Hidden"/>
         </Grid>
         <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="True">
               <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
               <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
               <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
               <Setter TargetName="Disabled" Property="Visibility" Value="Visible"/>
            </Trigger>
         </ControlTemplate.Triggers>
      </ControlTemplate>
   </Button.Template>
</Button>

1 Answer 1

2

You add a panel control in your page, then in your code behind use this:

ImageButton imgBtn = new ImageButton();
imgBtn.ID = "image_id";
imgBtn.ImageUrl = "your_image_path";
Panel1.Controls.Add (imgBtn);

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

You are using an Image as a button, but what I'm trying to achieve is to set an image inside a button without the need to override the original mouse properties. anyway, one vote for you.
Because your Question is ImageButton, so... If you want to change the image of he button (background?) you can do something like this: Button1.Attributes.Add("class", "className");, in the className (css) you define the image for the button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.