C#自定義WPF中Slider的Autotooltip模板
Slider控件有一個(gè)我比較喜歡的屬性"AutoToolTip",可以在拖動(dòng)的過(guò)程中顯示當(dāng)前刻度,然而這個(gè)刻度卻不支持模板定制,并且就連自定義格式也不行。這就大大的限制了它的使用范圍。網(wǎng)上有篇文章解決了這個(gè)問(wèn)題,可以實(shí)現(xiàn)自定義顯示格式
代碼如下:
/// <summary>
/// A Slider which provides a way to modify the
/// 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;
}
}
}使用起來(lái)也很簡(jiǎn)單。
<local:FormattedSlider
AutoToolTipFormat="{}{0}% used"
AutoToolTipPlacement="BottomRight" />其實(shí)原理也不復(fù)雜,通過(guò)反射設(shè)置"_autoToolTip"變量,從而實(shí)現(xiàn)自定義AutoToolTip格式
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;
}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#實(shí)現(xiàn)WPF項(xiàng)目復(fù)制和移動(dòng)文件夾
- C#中WPF顏色對(duì)話(huà)框控件的實(shí)現(xiàn)
- C# WPF開(kāi)源UI控件庫(kù)MaterialDesign介紹
- C#?WPF數(shù)據(jù)綁定模板化操作的完整步驟
- C#?wpf?通過(guò)HwndHost渲染視頻的實(shí)現(xiàn)方法
- C# wpf簡(jiǎn)單顏色板的實(shí)現(xiàn)
- C# WPF實(shí)現(xiàn)的語(yǔ)音播放自定義控件
- C# WPF如何反射加載Geometry幾何圖形數(shù)據(jù)圖標(biāo)
- c# wpf如何附加依賴(lài)項(xiàng)屬性
相關(guān)文章
C#模擬實(shí)現(xiàn)鼠標(biāo)自動(dòng)點(diǎn)擊與消息發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了C#如何利用windows api來(lái)模擬實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊、右擊、雙擊以及發(fā)送文本功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08
C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在
本文列舉了C#中文件操作中常用的函數(shù),創(chuàng)建文件和判斷文件存不存在的基本使用,簡(jiǎn)單實(shí)用,希望能幫到大家。2016-05-05
c#實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能示例分享
這篇文章主要介紹了c#實(shí)現(xiàn)的斷點(diǎn)續(xù)傳功能示例,斷點(diǎn)續(xù)傳就是在上一次下載時(shí)斷開(kāi)的位置開(kāi)始繼續(xù)下載。在HTTP協(xié)議中,可以在請(qǐng)求報(bào)文頭中加入Range段,來(lái)表示客戶(hù)機(jī)希望從何處繼續(xù)下載,下面是示例,需要的朋友可以參考下2014-03-03
關(guān)于C#執(zhí)行順序帶來(lái)的一些潛在問(wèn)題
這篇文章主要給大家介紹了關(guān)于C#執(zhí)行順序帶來(lái)的一些潛在問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
C#中WPF顏色對(duì)話(huà)框控件的實(shí)現(xiàn)
在 C# WPF開(kāi)發(fā)中顏色對(duì)話(huà)框控件(ColorDialog)用于對(duì)界面中的背景、文字…(擁有顏色屬性的所有控件)設(shè)置顏色,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

