【问题标题】:Create Image Button using Code-behind使用代码隐藏创建图像按钮
【发布时间】:2011-03-22 09:05:05
【问题描述】:

我正在尝试在运行时创建一个三态图像按钮。 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>

【问题讨论】:

    标签: c# image button wpf-controls


    【解决方案1】:

    你在你的页面中添加一个面板控件,然后在你的代码后面使用这个:

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

    希望对你有帮助

    【讨论】:

    • 您正在使用图像作为按钮,但我想要实现的是在按钮内设置图像,而无需覆盖原始鼠标属性。无论如何,给你一票。
    • 因为你的问题是 ImageButton,所以......如果你想改变他按钮的图像(背景?)你可以这样做:Button1.Attributes.Add("class", "className");,在你定义按钮的图像。
    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多