繁体   English   中英

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

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

我对WPF还是陌生的(用它编写我的第一个应用程序),但是基本上我想(或者认为我想要)创建一个具有3个图像(空闲,悬停,onClick)的某种控件,但是我可以更改图像。 所以现在我有:

                <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>

而且效果很好。 但是我想将图形与代码分开,并且要使此工作有效,我在C#代码中手动交换了填充图像。 我看过类似的代码:

<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>

所以我认为这就是我想要的。 但理想情况下,我将拥有许多这些按钮,因此我可以创建一些东西来做到这一点:

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

第一种解决方案:您可以为按钮创建控件模板(如果打算仅在按钮内显示图像,则可以创建样式),以声明将用于重叠/单击/其他操作的图像。 该样式将由所需的所有按钮共享,因为它将具有指定的键。

第二种解决方案:您可以将按钮子类化,并为悬停/空闲/单击的图像声明3个依赖项属性。 然后,对于子类按钮,将需要使用该依赖项属性的一些默认模板(如果按钮仅显示图像,则为样式)。

暂无
暂无

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

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