我正在尝试在运行时创建一个三态图像按钮。 XAML中有现有的方法,但我想知道如何在后面的代码中执行它(即如何将以下代码转换为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>
答案 0 :(得分:2)
您在页面中添加了一个面板控件,然后在您的代码中使用它:
ImageButton imgBtn = new ImageButton();
imgBtn.ID = "image_id";
imgBtn.ImageUrl = "your_image_path";
Panel1.Controls.Add (imgBtn);
希望这有帮助