基础知识(十三)WPF中点击按钮后,切换ImageButton 的几种状态

一、简介

    这篇博客记录了点击按钮后,切换ImageButton 的几种状态,请看点击步骤。

点击头部:

然后点击肩部:

二、工程目录

2.1、MainWindow.xaml

<Window x:Class="WPFtest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFtest"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="600"  Background="{x:Null}">

    <Viewbox>
        <Canvas Name="容器" Width="200" Height="400">
            <Path x:Name="Body" Fill="#FF717882" Data="M1078.5728,5787.0056 C1077.3367,5793.2093 1037.683,5811.0056 931.68296,5842.0056 863.68296,5863.0056 858.68296,5866.0056 852.68296,5896.0056 849.68296,5914.0056 840.68296,5933.0056 832.68296,5940.0056 818.68296,5951.0056 597.68296,5962.0056 566.68296,5953.0056 546.68296,5947.0056 543.68296,5923.0056 557.68296,5886.0056 566.68296,5862.0056 564.68296,5861.0056 505.68296,5844.0056 387.68296,5811.0056 288.68296,5746.0056 252.68296,5677.0056 235.68296,5644.0056 234.68296,5582.0056&#xD;&#xA;251.68296,5553.0056 269.68296,5521.0056 337.26475,5449.6905 349.21909,5448.6446 349.68887,5448.6035 350.1294,5447.9233 350.1294,5447.6123 350.1294,5435.6123 500.68296,5382.4461 572.68296,5369.4461 605.68296,5364.4461 698.68296,5356.3757 698.68296,5356.3757 L698.68296,5191.0056 C698.68296,4899.0056 721.68296,4652.0056 774.68296,4374.0056 806.68296,4201.0056 810.68296,4154.0056 803.68296,3959.0056 790.68296,3603.0056 792.68296,3440.0056 811.68296,3381.0056 820.68296,3352.0056 827.68296,3326.0056 827.68296,3324.0056 826.68296,3279.0056 866.68296,3094.0056 907.68296,2953.0056 950.68296,2805.0056 955.68296,2778.0056 977.68296,2604.0056 995.68296,2462.0056 999.68296,2385.0056 998.68296,2154.0056 998.68296,2121.0056 999.68296,2069.0056&#xD;&#xA;1001.683,2039.0056 1002.683,2009.0056 997.68296,1950.0056 990.68296,1909.0056 983.68296,1868.0056 980.68296,1817.0056 984.68296,1797.0056 991.68296,1752.0056 985.68296,1751.0056 922.68296,1784.0056 896.68296,1798.0056 864.68296,1809.0056 851.68296,1809.0056 833.68296,1809.0056 824.68296,1818.0056 812.68296,1847.0056 804.68296,1867.0056 793.68296,1893.0056 789.68296,1904.0056 784.68296,1915.0056 771.68296,1951.0056 758.68296,1984.0056 731.68296,2059.0056&#xD;&#xA;737.68296,2045.0056 627.68296,2288.0056 577.68296,2401.0056 518.68296,2526.0056 497.68296,2566.0056 L458.68296,2640.0056 480.68296,2717.0056 C509.68296,2820.0056 511.68296,3041.0056 484.68296,3138.0056 431.68296,3330.0056 318.68296,3443.0056 210.68296,3411.0056 154.68296,3394.0056 77.682957,3309.0056 45.682957,3229.0056 4.6829566,3124.0056 -4.3170434,3055.0056 1.6829566,2891.0056 8.6829566,2690.0056 43.682957,2592.0056 141.68296,2502.0056 183.68296,2463.0056 201.68296,2456.0056 277.68296,2446.0056 285.68296,2445.0056 296.68296,2422.0056 304.68296,2394.0056 319.68296,2335.0056 398.68296,2119.0056&#xD;&#xA;432.68296,2045.0056 517.68296,1860.0056 588.68296,1673.0056 608.68296,1584.0056 614.68296,1559.0056 613.68296,1523.0056 606.68296,1486.0056 589.68296,1396.0056 601.68296,1147.0056 626.68296,1078.0056 655.68296,997.00556 675.68296,962.00556 721.68296,911.00556 811.68296,814.00556 895.68296,816.00556 983.68296,918.00556 1040.683,983.00556 1059.683,982.00556 1184.683,908.00556 L1286.683,847.00556 1293.683,764.00556 C1300.683,675.00556 1297.683,659.00556 1270.683,659.00556 1261.683,659.00556 1224.683,645.00556 1188.683,628.00556 1120.683,596.00556 1056.683,543.00556 1032.683,497.00556 1015.683,464.00556 1014.683,402.00556 1031.683,373.00556 1049.683,341.00556 1116.683,269.00556 1128.683,269.00556 1133.683,269.00556 1138.683,265.00556 1138.683,261.00556 1138.683,256.00556 1156.683,246.00556 1178.683,239.00556 1226.683,223.00556 1229.683,220.00556&#xD;&#xA;1252.683,148.00556 1266.683,104.00556 1280.683,84.005556 1312.683,60.005556 1368.683,16.005556 1445.683,-4.9944441 1528.683,1.0055559 1607.683,7.0055559 1649.683,26.005556 1710.683,82.005556 1748.683,117.00556 1757.683,132.00556 1762.683,171.00556 1769.683,215.00556 1772.683,218.00556 1823.683,243.00556 1883.683,273.00556 1955.683,339.00556 1973.683,382.00556 1981.683,401.00556 1982.683,427.00556 1977.683,461.00556 1970.683,502.00556&#xD;&#xA;1961.683,519.00556 1924.683,555.00556 1899.683,579.00556 1874.683,599.00556 1868.683,599.00556 1863.683,599.00556 1858.683,603.00556 1858.683,608.00556 1858.683,613.00556 1848.683,619.00556 1836.683,622.00556 1823.683,625.00556 1784.683,638.00556 1748.683,652.00556 L1683.683,677.00556 1680.683,767.00556 C1677.683,854.00556 1678.683,858.00556 1705.683,883.00556 1720.683,897.00556 1760.683,922.00556 1795.683,939.00556 1848.683,965.00556 1876.683,972.00556 1971.683,979.00556 2105.683,990.00556 2177.683,1008.0056 2224.683,1044.0056 2276.683,1084.0056 2291.683,1122.0056 2295.683,1224.0056 2298.683,1283.0056 2306.683,1328.0056 2318.683,1354.0056 2348.683,1420.0056 2356.683,1451.0056 2364.683,1519.0056 2367.683,1554.0056 2377.683,1594.0056&#xD;&#xA;2385.683,1606.0056 2393.683,1618.0056 2406.683,1648.0056 2413.683,1671.0056 2424.683,1709.0056 2429.683,1715.0056 2459.683,1720.0056 2532.683,1732.0056 2592.683,1789.0056 2643.683,1894.0056 2692.683,1995.0056 2706.683,2079.0056 2701.683,2234.0056 2694.683,2426.0056 2696.683,2453.0056 2719.683,2548.0056 2783.683,2814.0056 2812.683,2891.0056 2886.683,2994.0056 2921.683,3043.0056 2922.683,3047.0056 2926.683,3150.0056 2929.683,3234.0056&#xD;&#xA;2927.683,3262.0056 2914.683,3287.0056 2897.683,3321.0056 2878.683,3329.0056 2878.683,3304.0056 2878.683,3284.0056 2871.683,3285.0056 2840.683,3309.0056 2811.683,3332.0056 2786.683,3321.0056 2808.683,3294.0056 2819.683,3281.0056 2817.683,3280.0056 2790.683,3285.0056 2763.683,3291.0056 2760.683,3289.0056 2765.683,3271.0056 2769.683,3254.0056 2766.683,3251.0056 2749.683,3256.0056 2727.683,3262.0056 2721.683,3250.0056 2738.683,3233.0056&#xD;&#xA;2743.683,3228.0056 2758.683,3202.0056 2772.683,3175.0056 L2797.683,3126.0056 2765.683,3088.0056 2733.683,3050.0056 2722.683,3110.0056 C2709.683,3180.0056 2704.683,3188.0056 2687.683,3158.0056 2679.683,3143.0056 2676.683,3086.0056 2677.683,2985.0056 L2678.683,2834.0056 2613.683,2742.0056 2549.683,2651.0056 2511.683,2670.0056 C2443.683,2705.0056 2388.683,2689.0056 2318.683,2613.0056 2234.683,2523.0056 2198.683,2401.0056 2199.683,2209.0056 2199.683,2140.0056 2203.683,2063.0056 2207.683,2037.0056 2212.683,2005.0056 2211.683,1980.0056 2203.683,1962.0056 2150.683,1841.0056 2134.683,1797.0056 2112.683,1713.0056 2099.683,1660.0056 2088.683,1599.0056 2088.683,1578.0056 2088.683,1513.0056 2064.683,1531.0056 2034.683,1619.0056 1992.683,1744.0056 1934.683,2073.0056&#xD;&#xA;1948.683,2113.0056 1951.683,2123.0056 1950.683,2160.0056 1945.683,2195.0056 1938.683,2245.0056 1940.683,2290.0056 1956.683,2401.0056 1967.683,2480.0056 1980.683,2580.0056 1983.683,2624.0056 1993.683,2747.0056 2005.683,2794.0056 2047.683,2888.0056 2098.683,3002.0056 2136.683,3190.0056 2158.683,3444.0056 2164.683,3510.0056 2171.683,3572.0056 2174.683,3582.0056 2177.683,3591.0056 2183.683,3707.0056 2187.683,3839.0056 2194.683,4060.0056&#xD;&#xA;2196.683,4081.0056 2215.683,4107.0056 2263.683,4171.0056 2297.683,4256.0056 2315.683,4354.0056 2328.683,4424.0056 2323.683,4640.0056 2307.683,4699.0056 2298.683,4732.0056 2294.683,4767.0056 2299.683,4786.0056 2315.683,4862.0056 2320.683,5165.0056 2307.683,5308.0056 2298.683,5422.0056 2297.683,5481.0056 2307.683,5577.0056 2324.683,5758.0056 2333.683,5784.0056 2395.683,5833.0056 2436.683,5865.0056 2448.683,5880.0056 2448.683,5901.0056&#xD;&#xA;2448.683,5928.0056 2446.683,5929.0056 2391.683,5930.0056 2359.683,5930.0056 2311.683,5935.0056 2283.683,5940.0056 2256.683,5946.0056 2217.683,5953.0056 2197.683,5956.0056 2155.683,5962.0056 2158.683,5967.0056 2138.683,5835.0056 2132.683,5797.0056 2120.683,5752.0056 2111.683,5735.0056 2099.683,5711.0056 2094.683,5668.0056 2092.683,5544.0056 2089.683,5368.0056 2078.683,5300.0056 2029.683,5175.0056 2011.683,5132.0056 1992.683,5064.0056&#xD;&#xA;1986.683,5025.0056 1976.683,4965.0056 1968.683,4947.0056 1935.683,4911.0056 1890.683,4861.0056 1854.683,4783.0056 1833.683,4695.0056 1815.683,4618.0056 1814.683,4406.0056 1832.683,4322.0056 1842.683,4272.0056 1842.683,4247.0056 1833.683,4202.0056 1815.683,4118.0056 1772.683,3960.0056 1733.683,3834.0056 1714.683,3774.0056 1680.683,3663.0056 1658.683,3589.0056 1635.683,3515.0056 1603.683,3411.0056 1586.683,3359.0056 1569.683,3307.0056&#xD;&#xA;1549.683,3227.0056 1541.683,3181.0056 1527.683,3103.0056 1527.683,3097.0056 1543.683,3079.0056 1560.683,3060.0056 1516.683,3064.0056 1512.683,3070.0056 1508.683,3077.0056 1490.683,3079.0056 1467.683,3076.0056 1425.8338,3058.5734 1416.683,3080.0056 1439.683,3094.0056 1456.683,3105.0056 1441.683,3172.0056 1386.683,3324.0056 1306.683,3549.0056 1301.683,3569.0056 1294.683,3669.0056 1291.683,3721.0056 1282.683,3789.0056 1274.683,3819.0056&#xD;&#xA;1258.683,3876.0056 1209.683,3990.0056 1195.683,4000.0056 1191.683,4003.0056 1173.683,4052.0056 1157.683,4109.0056 1141.683,4166.0056 1113.683,4246.0056 1097.683,4288.0056 1069.683,4357.0056 1066.683,4377.0056 1062.683,4499.0056 1049.683,4910.0056 1054.683,4878.0056 984.68296,5077.0056 964.68296,5133.0056 939.68296,5224.0056 929.68296,5279.0056 L912.68296,5378.0056 942.68296,5385.0056 C1000.683,5397.0056 1095.683,5449.0056 1138.683,5491.0056 1195.683,5548.0056 1207.683,5578.0056 1197.683,5641.0056 1190.683,5682.0056 1181.683,5699.0056 1144.683,5735.0056 1119.683,5759.0056 1094.6173,5778.1205 1088.683,5779.0056" Height="384.477" Canvas.Left="6.373" Canvas.Top="8.494" Width="188.931" Stretch="Fill"/>
            <local:ImageButton x:Name="btnHead" Canvas.Left="70.749" Canvas.Top="19.781" Visibility="Visible" Width="66" Height="36" DefaultImage="{Binding HeadCanvas}" Click="HeadClick"/>
            <local:ImageButton x:Name="btnShouder" Canvas.Left="43" Canvas.Top="60" Visibility="Visible" Width="36" Height="66" DefaultImage="{Binding ShoulderCanvas}" Click="ShoulderClick"/>
        </Canvas>
    </Viewbox>
</Window>

2.2、MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WPFtest
{
    /// <summary>
    /// Window2.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public MainWindow()
        {
            this.DataContext = this;
            InitializeComponent();
            HeadCanvas = (Canvas)Application.Current.FindResource("HeadDefualt");
            ShoulderCanvas = (Canvas)Application.Current.FindResource("ShouderDefualt");
        }

        private Canvas _headCanvas;
        /// <summary>
        /// 头部
        /// </summary>
        public Canvas HeadCanvas
        {
            get { return _headCanvas; }
            set { _headCanvas = value; RaisePropertyChanged("HeadCanvas"); }
        }

        private Canvas _shoulderCanvas;
        /// <summary>
        /// 肩部
        /// </summary>
        public Canvas ShoulderCanvas
        {
            get { return _shoulderCanvas; }
            set { _shoulderCanvas = value; RaisePropertyChanged("ShoulderCanvas"); }
        }

        public void HeadClick(object sender, RoutedEventArgs e)
        {
            HeadCanvas = (Canvas)Application.Current.FindResource("HeadSelected");
            ShoulderCanvas = (Canvas)Application.Current.FindResource("ShouderDefualt");
        }

        public void ShoulderClick(object sender, RoutedEventArgs e)
        {
            HeadCanvas = (Canvas)Application.Current.FindResource("HeadDefualt");
            ShoulderCanvas = (Canvas)Application.Current.FindResource("ShouderSelected");
        }
    }
}

2.3、ImageButtonResource.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
                    xmlns:local="clr-namespace:WPFtest">

    <Canvas Width="66" Height="36" Canvas.Left="70.749" Canvas.Top="19.781" x:Key="HeadDefualt">
        <Path x:Name="头部椭圆_Defualt" Fill="#FF717882" Height="36" Stretch="Fill" Width="66" Canvas.Left="0" Canvas.Top="0" Data="m 121.7112 135.32026 c -1.08479 -0.47625 -2.72521 -2.32834 -3.46604 -3.96875 -3.09563 -6.77334 -1.53459 -17.51542 2.91041 -20.1348 3.81 -2.24896 7.54063 1.87854 8.49313 9.39271 0.82021 6.35 -1.56104 13.25563 -5.08 14.71084 -1.05834 0.44979 -1.82563 0.44979 -2.8575 0 z" Stroke="#FF398848" StrokeThickness="0" />
        <Path x:Name="头部_Defualt" Fill="#FFBBBEC3" StrokeThickness="0.10708148" Data="M124.05187,118.47762 C122.88439,118.26153 120.86783,117.82939 119.59422,117.61332 117.89609,117.18117 116.83472,115.77669 115.87953,112.42754 115.24272,109.94268 112.58935,105.51317 110.14827,102.59617 103.78021,95.141608 102.5066,86.822752 107.70719,86.822752 109.51146,86.822752 109.72371,86.066494 109.72371,80.664639 109.72371,71.049342 110.25441,70.077008 115.87953,68.564488 129.57084,65.107304 147.18917,65.594662 161.09277,69.15989 L166.29336,70.943693 165.76265,79.476238 C165.23199,88.335276 165.33813,88.983499 167.24855,88.983499 168.30988,88.983499 169.47739,89.739757 169.79577,90.604055 170.64486,92.872834 167.35469,102.48813 164.9136,104.64887 163.74613,105.72925 161.62343,109.07839 160.24368,112.21147 157.80259,117.61332 157.59031,117.8294 152.70814,118.47762 147.29527,119.23388 127.55428,119.23388 124.05187,118.47762 z" Height="32.873" Stretch="Fill" Width="42.086" Canvas.Left="10.666" Canvas.Top="1.332"/>
    </Canvas>
    <Canvas Width="66" Height="36" Canvas.Left="70.749" Canvas.Top="19.781" x:Key="HeadSelected">
        <Path Name="头部椭圆" Fill="#FF8EC5F3" Height="36" Stretch="Fill" Width="66" Canvas.Left="0" Canvas.Top="0" Data="m 121.7112 135.32026 c -1.08479 -0.47625 -2.72521 -2.32834 -3.46604 -3.96875 -3.09563 -6.77334 -1.53459 -17.51542 2.91041 -20.1348 3.81 -2.24896 7.54063 1.87854 8.49313 9.39271 0.82021 6.35 -1.56104 13.25563 -5.08 14.71084 -1.05834 0.44979 -1.82563 0.44979 -2.8575 0 z" Stroke="#FF398848" />
        <Path Name="头部" Fill="White" StrokeThickness="0.10708148" Data="M124.05187,118.47762 C122.88439,118.26153 120.86783,117.82939 119.59422,117.61332 117.89609,117.18117 116.83472,115.77669 115.87953,112.42754 115.24272,109.94268 112.58935,105.51317 110.14827,102.59617 103.78021,95.141608 102.5066,86.822752 107.70719,86.822752 109.51146,86.822752 109.72371,86.066494 109.72371,80.664639 109.72371,71.049342 110.25441,70.077008 115.87953,68.564488 129.57084,65.107304 147.18917,65.594662 161.09277,69.15989 L166.29336,70.943693 165.76265,79.476238 C165.23199,88.335276 165.33813,88.983499 167.24855,88.983499 168.30988,88.983499 169.47739,89.739757 169.79577,90.604055 170.64486,92.872834 167.35469,102.48813 164.9136,104.64887 163.74613,105.72925 161.62343,109.07839 160.24368,112.21147 157.80259,117.61332 157.59031,117.8294 152.70814,118.47762 147.29527,119.23388 127.55428,119.23388 124.05187,118.47762 z" Height="32.871" Stretch="Fill" Width="42.085" Canvas.Left="11.332" Canvas.Top="1.665"/>
    </Canvas>

    <Canvas Width="36" Height="66" x:Key="ShouderDefualt">
        <Path x:Name="肩部椭圆_defualt" Fill="#FF717882" Height="66" Stretch="Fill" Width="36" Canvas.Left="0" Canvas.Top="0" Data="m 121.7112 135.32026 c -1.08479 -0.47625 -2.72521 -2.32834 -3.46604 -3.96875 -3.09563 -6.77334 -1.53459 -17.51542 2.91041 -20.1348 3.81 -2.24896 7.54063 1.87854 8.49313 9.39271 0.82021 6.35 -1.56104 13.25563 -5.08 14.71084 -1.05834 0.44979 -1.82563 0.44979 -2.8575 0 z" Stroke="#FF398848" StrokeThickness="0" />
        <Path x:Name="肩部__defualt" Fill="#FFBBBEC3" StrokeThickness="0" Data="M121.12801,135.17832 C119.8848,134.44866 118.44513,132.45419 117.39109,130.15706 116.6884,128.58962 116.66138,128.53556 117.01273,126.23845 117.66137,121.67123 118.8235,117.67152 119.93159,116.10408 121.25588,114.21234 122.36398,113.50967 124.87744,113.07728 127.01254,112.72594 127.1747,112.72594 127.499,113.15848 128.09359,113.99624 130.02046,118.24054 130.2637,120.48362 130.6691,124.51034 129.05952,130.21358 127.38389,132.67285 L126.97768,133.46288 126.58859,133.77203 126.38066,133.46895 126.17849,131.79998 C125.85418,130.25954 124.75716,128.43086 124.37879,128.6741 124.10853,128.83624 123.56799,130.72798 122.91936,133.72777 122.67612,134.86282 122.40586,135.80869 122.29774,135.80869 122.18964,135.80869 121.74962,135.55679 121.12801,135.17845 z" Height="53.895" Stretch="Fill" Width="30.826" Canvas.Top="6.105" Canvas.Left="2.174"/>
    </Canvas>
    <Canvas Width="36" Height="66" x:Key="ShouderSelected">
        <Path Name="肩部椭圆" Fill="#FF8EC5F3" Height="66" Stretch="Fill" Width="36" Canvas.Left="0" Canvas.Top="0" Data="m 121.7112 135.32026 c -1.08479 -0.47625 -2.72521 -2.32834 -3.46604 -3.96875 -3.09563 -6.77334 -1.53459 -17.51542 2.91041 -20.1348 3.81 -2.24896 7.54063 1.87854 8.49313 9.39271 0.82021 6.35 -1.56104 13.25563 -5.08 14.71084 -1.05834 0.44979 -1.82563 0.44979 -2.8575 0 z" Stroke="#FF398848" />
        <Path Name="肩部" Fill="White" StrokeThickness="0" Data="M121.12801,135.17832 C119.8848,134.44866 118.44513,132.45419 117.39109,130.15706 116.6884,128.58962 116.66138,128.53556 117.01273,126.23845 117.66137,121.67123 118.8235,117.67152 119.93159,116.10408 121.25588,114.21234 122.36398,113.50967 124.87744,113.07728 127.01254,112.72594 127.1747,112.72594 127.499,113.15848 128.09359,113.99624 130.02046,118.24054 130.2637,120.48362 130.6691,124.51034 129.05952,130.21358 127.38389,132.67285 L126.97768,133.46288 126.58859,133.77203 126.38066,133.46895 126.17849,131.79998 C125.85418,130.25954 124.75716,128.43086 124.37879,128.6741 124.10853,128.83624 123.56799,130.72798 122.91936,133.72777 122.67612,134.86282 122.40586,135.80869 122.29774,135.80869 122.18964,135.80869 121.74962,135.55679 121.12801,135.17845 z" Height="53.895" Stretch="Fill" Width="30.823" Canvas.Top="6.715" Canvas.Left="2.396"/>
    </Canvas>


</ResourceDictionary>

2.4、ImageButtonStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WPFtest">
    <Style TargetType="{x:Type local:ImageButton}" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ImageButton}">
                    <Border   Name="border" BorderThickness="0" BorderBrush="Transparent">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <VisualBrush Visual="{Binding Path=DefaultImage, RelativeSource={RelativeSource TemplatedParent}}"></VisualBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

2.5、ImageButton.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WPFtest
{
    public class ImageButton : Button
    {
        static ImageButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
        }

        public static readonly DependencyProperty DefaultImageProperty =
                                                        DependencyProperty.Register("DefaultImage",
                                                                                    typeof(Canvas),
                                                                                    typeof(ImageButton),
                                                                                    new FrameworkPropertyMetadata());
        public static readonly DependencyProperty HoverImageProperty =
                                                        DependencyProperty.Register("HoverImage",
                                                                                    typeof(Canvas),
                                                                                    typeof(ImageButton),
                                                                                    new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty PressedImageProperty =
                                                        DependencyProperty.Register("PressedImage",
                                                                                    typeof(Canvas),
                                                                                    typeof(ImageButton),
                                                                                    new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty DisabledImageProperty =
                                                        DependencyProperty.Register("DisabledImage",
                                                                                    typeof(Canvas),
                                                                                    typeof(ImageButton),
                                                                                    new FrameworkPropertyMetadata(null));

        public static readonly DependencyProperty SelectedImageProperty =
                                                  DependencyProperty.Register("SelectedImage",
                                                                              typeof(Canvas),
                                                                              typeof(ImageButton),
                                                                              new FrameworkPropertyMetadata(null));

        public Canvas DefaultImage
        {
            get { return (Canvas)GetValue(DefaultImageProperty); }
            set { SetValue(DefaultImageProperty, value); }
        }

        public Canvas HoverImage
        {
            get { return (Canvas)FindResource(GetValue(HoverImageProperty)); }
            set { SetValue(HoverImageProperty, value); }
        }

        public Canvas PressedImage
        {
            get { return (Canvas)GetValue(PressedImageProperty); }
            set { SetValue(PressedImageProperty, value); }
        }

        public Canvas DisabledImage
        {
            get { return (Canvas)GetValue(DisabledImageProperty); }
            set { SetValue(DisabledImageProperty, value); }
        }

        public Canvas SelectedImage
        {
            get { return (Canvas)GetValue(SelectedImageProperty); }
            set { SetValue(SelectedImageProperty, value); }
        }
    }
}

2.6、App.xaml

<Application x:Class="WPFtest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFtest"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ImageButtonResource.xaml"/>
                <ResourceDictionary Source="/ImageButtonStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF,如果你想要设置TabControlTabItem的默认背景色,并且希望在按钮点击后颜色发生变化,你可以按照以下步骤操作: 1. **设置默认背景色**: 首先,在你的项目资源文件(例如:`Resources.xaml`)添加一个静态资源,给TabItem定义一个默认的Background颜色: ```xml <SolidColorBrush x:Key="DefaultTabColor" Color="#F0F0F0" /> ``` 然后,在TabControl的样式(`TabControl.xaml` 或 `App.xaml` 的 `<Style TargetType="TabControl">` 内部)应用这个背景色作为模板: ```xml <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TabControl"> <Border Background="{StaticResource DefaultTabColor}"> <ItemsPresenter/> </Border> </ControlTemplate> </Setter.Value> </Setter> ``` 2. **按钮点击事件处理**: 在XAML,为需要改变背景色的按钮添加一个Click事件处理器。比如,当你点击一个名为`ChangeColorButton`的按钮时,可以将当前选的TabItem的背景色更改为另一种颜色: ```xml <Button Content="Change Color" Click="ChangeColorButton_Click"/> private void ChangeColorButton_Click(object sender, RoutedEventArgs e) { TabItem selectedItem = this.TabControl.SelectedItem as TabItem; if (selectedItem != null) { SolidColorBrush newColor = (SolidColorBrush)Application.Current.Resources["YourNewColorBrush"]; selectedItem.Background = newColor; } } ``` 其,`YourNewColorBrush`是你在资源文件定义的新颜色,如`<SolidColorBrush x:Key="NewTabColor" Color="#FF45B8FF" />`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值