【问题标题】:How to make an Image work like a button?如何使图像像按钮一样工作?
【发布时间】:2015-12-20 05:29:06
【问题描述】:

我已经开发了一个带有选项卡控件的应用程序,我目前有 4 个选项卡,我想使用我创建的箭头图像在它们之间导航。所以我想知道是否有办法让我将该图像变成一个功能按钮,当我点击它时会切换标签?如果有,怎么做?

谢谢

【问题讨论】:

  • 你可以把你的图片放在你的按钮里。或者,如果您不想要默认按钮镶边,请将按钮的模板更改为图像。如需更具体的答案,您可以提出更具体的问题。
  • 如果 OP 使用它,我确信 Google 会在第一个结果中给出解决方案。

标签: c# wpf image button


【解决方案1】:

在 google 中搜索你会得到很多选项,无论如何它就像代码 sn-p 所示的示例。

您可以对其进行更多自定义,但我留给您探索。

<Button>
    <Image Source="yourimagepath"></Image>
</Button>

【讨论】:

    【解决方案2】:

    将图像添加到按钮并使用下面给出的样式覆盖按钮的默认镶边

       <Button HorizontalAlignment="Left" Margin="33,260,0,0" VerticalAlignment="Top" Width="84" Click="Button_Click" Height="29">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <ContentPresenter                  
                            Margin="{TemplateBinding Padding}"                  
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                  
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                  
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                  
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            RecognizesAccessKey="True"                  
                            Content="{TemplateBinding Content}" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Button.Style>
            <Button.Content>
                <Image Source="imagepath.extension"/>
            </Button.Content>
        </Button>
    

    对于导航,您可以使用选项卡控件的 SelectedIndex 属性,如下所示

    private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (tab1.SelectedIndex + 1 < tab1.Items.Count)
            {
                tab1.SelectedIndex = tab1.SelectedIndex + 1;
            }
            else
            {
                tab1.SelectedIndex = 0;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 2015-01-05
      • 2013-04-14
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多