简体   繁体   English

具有可变图像的简单WPF按钮控制模板?

[英]Simple WPF Button Control Template with variable images?

I am still new to WPF (writing my first app with it), but basically I am wanting (or think I want) to create a control of somekind that has 3 images (idle, hover, onClick) but that I can change the images. 我对WPF还是陌生的(用它编写我的第一个应用程序),但是基本上我想(或者认为我想要)创建一个具有3个图像(空闲,悬停,onClick)的某种控件,但是我可以更改图像。 So now I have: 所以现在我有:

                <Rectangle Height="29" Width="35" Margin="0,2,0,0"
                       HorizontalAlignment="Left" VerticalAlignment="Top" 
                       Name="BTN_OpenDeviceSettings" ToolTip="Open Device Settings"
                       Fill="{DynamicResource device_grid___open_grid}"
                       MouseEnter="BTN_OpenDeviceSettings_MouseEnter"
                       MouseLeave="BTN_OpenDeviceSettings_MouseLeave"
                       MouseLeftButtonDown="BTN_OpenDeviceSettings_MouseLeftButtonDown"
                       MouseLeftButtonUp="BTN_OpenDeviceSettings_MouseLeftButtonUp">
                </Rectangle>

And it works great. 而且效果很好。 But I want to separate the graphics from the code, and to make this work I manually swap the fill image in my C# code. 但是我想将图形与代码分开,并且要使此工作有效,我在C#代码中手动交换了填充图像。 I have seen code like: 我看过类似的代码:

<Button Name="btnNext" Padding="15,3" HorizontalAlignment="Right" Click="OnButtonClick">
    <Image Width="90" Height="90">
        <Image.Style>
            <Style TargetType="Image">
                <Setter Property="Source" Value="/resources/a.png" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Source" Value="/resources/b.png" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
</Button>

So I think that is what I am wanting. 所以我认为这就是我想要的。 But ideally, I am going to have many of these buttons, so can I create something so that I do this: 但理想情况下,我将拥有许多这些按钮,因此我可以创建一些东西来做到这一点:

<MyButton>
  <Images idleImage="someimage.png" hoverImage="someOtherImage.png" clickImage="someOtherOtherImage.png" onCLick="some_cSharp_method_to_call" />
</MyButton>

First solution: you can create a control template(or style if you are planning to show only images within a button) for buttons which will declare what images will be used for overlapping/clicking/other operations. 第一种解决方案:您可以为按钮创建控件模板(如果打算仅在按钮内显示图像,则可以创建样式),以声明将用于重叠/单击/其他操作的图像。 This style will be shared by all buttons you want because it will have a designated key. 该样式将由所需的所有按钮共享,因为它将具有指定的键。

Second solution: you can subclass button and declare 3 dependency properties for hover/idle/clicked images. 第二种解决方案:您可以将按钮子类化,并为悬停/空闲/单击的图像声明3个依赖项属性。 Then again you will need some default template(or style if button only displays images) for your subclassed button which will use that dependency properties. 然后,对于子类按钮,将需要使用该依赖项属性的一些默认模板(如果按钮仅显示图像,则为样式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM