Customize the slider autotooltip template in WPF

Source: Internet
Author: User

The slider control has an attribute "autotooltip" that I like most. The current scale can be displayed during the drag process. However, this scale does not support template customization, and even the custom format does not work. This greatly limits the scope of use. There is an article on the Internet that modifying the auto tooltip of a slider (this address cannot be accessed due to WordPress being harmonious) solves this problem and allows you to customize the display format.

The Code is as follows:

Code
/** // <Summary>
/// A slider which provides a way to modify
/// Auto tooltip text by using a format string.
/// </Summary>
Public class formattedslider: Slider
{
Private tooltip _ autotooltip;
Private string _ autotooltipformat;

/** // <Summary>
/// Gets/sets a format string used to modify the auto tooltip's content.
// Note: This format string must contain exactly one placeholder value,
/// Which is used to hold the tooltip's original content.
/// </Summary>
Public String autotooltipformat
{
Get {return _ autotooltipformat ;}
Set {_ autotooltipformat = value ;}
}

Protected override void onthumbdragstarted (dragstartedeventargs E)
{
Base. onthumbdragstarted (E );
This. formatautotooltipcontent ();
}

Protected override void onthumbdragdelta (dragdeltaeventargs E)
{
Base. onthumbdragdelta (E );
This. formatautotooltipcontent ();
}

Private void formatautotooltipcontent ()
{
If (! String. isnullorempty (this. autotooltipformat ))
{
This. autotooltip. content = string. Format (
This. autotooltipformat,
This. autotooltip. content );
}
}

Private tooltip autotooltip
{
Get
{
If (_ autotooltip = NULL)
{
Fieldinfo field = typeof (slider). getfield (
"_ Autotooltip ",
Bindingflags. nonpublic | bindingflags. instance );

_ Autotooltip = field. getvalue (this) as tooltip;
}

Return _ autotooltip;
}
}
}

 

It is easy to use.
<Local: formattedslider
Autotooltipformat = "{}{ 0} % used"
Autotooltipplacement = "bottomright"/>

In fact, the principle is not complex. You can set the "_ autotooltip" variable through reflection to customize the autotooltip format.

Private tooltip autotooltip
{
Get
{
If (_ autotooltip = NULL)
{
Fieldinfo field = typeof (slider). getfield (
"_ Autotooltip ",
Bindingflags. nonpublic | bindingflags. instance );

_ Autotooltip = field. getvalue (this) as tooltip;
}

Return _ autotooltip;
}
}

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.