Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单的WPF按钮控制模板与可变图像?_C#_Wpf_Button_Controls_Controltemplate - Fatal编程技术网

C# 简单的WPF按钮控制模板与可变图像?

C# 简单的WPF按钮控制模板与可变图像?,c#,wpf,button,controls,controltemplate,C#,Wpf,Button,Controls,Controltemplate,我还不熟悉WPF(用它编写我的第一个应用程序),但基本上我想要(或认为我想要)创建一个控件,它有3个图像(空闲、悬停、onClick),但我可以更改图像。所以现在我有: <Rectangle Height="29" Width="35" Margin="0,2,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"

我还不熟悉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个依赖属性。然后,对于将使用该依赖属性的子类按钮,您还需要一些默认模板(或样式,如果按钮仅显示图像)。

听起来不错,但我不知道如何在WPF中实际实现它。我几乎可以用C#做任何事情,但WPF是一种奇怪的野兽,我正在学习。我对解决方案#2很感兴趣,因为如果我要创建一个WPF版本的UserControl,上面有一个列表框和一些文本,并且我想传递文本是什么(只是一个示例),这似乎会很有用。您是否有任何示例代码或参考来帮助解释#2的含义?