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" Prop开发者_开发问答erty="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>
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
精彩评论