WPF style and Template: The background color of the button does not change when the mouse is moved/suspended

Source: Internet
Author: User

Situation: The mouse moves to the button, the default is the button background color will change, the Internet can also search a lot of how to customize the change of the background color.

Requirements: Now the demand, in turn, wants to move the mouse over the button and keep the background color of the button unchanged.

One idea: In a style file, use MultiTrigger to define the background color of the button's "mouse hover + unchecked/selected" state.

Workaround: Customize a ToggleButton style file.
Mytogglebutton.xaml:

<resourcedictionary x:class= "HomeDecorationPSD.Presentation.Style.MyToggleButton" xmlns= "Http://schemas.micro Soft.com/winfx/2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:d=" htt p://schemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http://schemas.openxmlformats.org/ markup-compatibility/2006 "xmlns:local=" Clr-namespace:homedecorationpsd.presentation.style "mc:Ignorable=" D "> <style x:key=" Mytogglebutton "targettype=" {x:type ToggleButton} "> <setter property=" Width "Va Lue= "/>" <setter property= "Height" value= "/>" <!--replace the default template--> <setter erty= "Template" > <Setter.Value> <controltemplate targettype= "{x:type ToggleButton}" "> <border cornerradius=" 3 "borderthickness=" 1 "background=" {templatebinding Background} "Border
    Brush= "{TemplateBinding BorderBrush}" >                    <contentpresenter horizontalalignment= "center" verticalalignment= "center"/>
        </Border> </ControlTemplate> </Setter.Value> </Setter> 
                <Style.Triggers> <!--Change the three-state background color--> <trigger property= "ischecked" value= "true" > <setter property= "Background" value= "#FFA1C3F3"/> </Trigger> <trig
            Ger property= "ischecked" value= "false" > <setter property= "Background" value= "#FF14B3FD"/> </Trigger> <!--mouse hover, background color unchanged--> <MultiTrigger> <multitr Igger. conditions> <condition property= "IsMouseOver" value= "true"/> <conditio n property= "ischecked" value= "true"/> </MultiTrigger.Conditions> <multitrigger .
     Setters>               <setter property= "Background" value= "#FFA1C3F3"/> </MultiTrigger.Setters>
                    </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <condition property= "IsMouseOver" value= "true"/> <condition property= "ischecked" Valu
                    E= "false"/> </MultiTrigger.Conditions> <MultiTrigger.Setters> <setter property= "Background" value= "#FF14B3FD"/> </MultiTrigger.Setters> &
 lt;/multitrigger> </Style.Triggers> </Style> </ResourceDictionary>

Use this style for ToggleButton:

<usercontrol x:class= "HomeDecorationPSD.Presentation.Views.SpaceView" xmlns= "Http://schemas.microsoft". Com/winfx/2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:d=" http://s chemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/  2006 "xmlns:vm=" Clr-namespace:homedecorationpsd.applications.viewmodels "mc:ignorable=" D "MinWidth=" 300 " minheight= > <!--Introducing style files--> <UserControl.Resources> <resourcedictionary&
            Gt <ResourceDictionary.MergedDictionaries> <resourcedictionary source= "/presentation/style/mytoggleb Utton.xaml "/><!--is the relative path of engineering--> </ResourceDictionary.MergedDictionaries> </resourced ictionary> </UserControl.Resources> <dockpanel margin= "Ten" > <stackpanel orientation= "V Ertical ">
        <wrappanel orientation= "Horizontal" margin= "5" > <togglebutton content= "Guest Restaurant" ischecked= "T Rue "style=" {StaticResource Mytogglebutton} "margin=" 5 "/> <togglebutton content=" living room "style=" {StaticR Esource Mytogglebutton} "margin=" 5 "/> <togglebutton content=" Restaurant "style=" {StaticResource MyToggleButt 
                on} "margin=" 5 "/> <togglebutton content=" bedroom "style=" {StaticResource Mytogglebutton} "margin=" 5 "/> <togglebutton content= "Test" style= "{StaticResource Mytogglebutton}" margin= "5"/> &LT;/WR appanel> </StackPanel> </DockPanel> </UserControl>

Operation Effect:

Summary: If you do not specify a x:key value for the style, all ToggleButton become used. To avoid this, use the x:key value to specify which style to use, otherwise the default style.
Learning about WPF Styles Reference this article: Why http://www.cnblogs.com/zhouyinhui/archive/2007/03/27/690431.html Use the template template, Actually in a certain degree know to see the answer: https://zhidao.baidu.com/question/2137628503185830468.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.