亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#自定義WPF中Slider的Autotooltip模板

 更新時間:2022年06月16日 16:50:28   作者:天方  
這篇文章介紹了C#自定義WPF中Slider的Autotooltip模板的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Slider控件有一個我比較喜歡的屬性"AutoToolTip",可以在拖動的過程中顯示當(dāng)前刻度,然而這個刻度卻不支持模板定制,并且就連自定義格式也不行。這就大大的限制了它的使用范圍。網(wǎng)上有篇文章解決了這個問題,可以實現(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;
        }
    }    
}

使用起來也很簡單。

<local:FormattedSlider
     AutoToolTipFormat="{}{0}% used"
    AutoToolTipPlacement="BottomRight" />

其實原理也不復(fù)雜,通過反射設(shè)置"_autoToolTip"變量,從而實現(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;
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#多線程TPL模式高級用法探秘

    C#多線程TPL模式高級用法探秘

    本文詳細(xì)講解了C#多線程TPL模式的高級用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實例詳解

    C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實例詳解

    這篇文章主要介紹了C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實例以及相關(guān)知識點內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2019-08-08
  • C#實現(xiàn)rar壓縮與解壓縮文件的方法

    C#實現(xiàn)rar壓縮與解壓縮文件的方法

    這篇文章主要介紹了C#實現(xiàn)rar壓縮與解壓縮文件的方法,實例分析了C#利用winrar程序?qū)崿F(xiàn)文件的壓縮與解壓縮的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#使用Aspose.Cells創(chuàng)建和讀取Excel文件

    C#使用Aspose.Cells創(chuàng)建和讀取Excel文件

    這篇文章主要為大家詳細(xì)介紹了C#使用Aspose.Cells創(chuàng)建和讀取Excel文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • C# 實現(xiàn)抓包的實例代碼

    C# 實現(xiàn)抓包的實例代碼

    這篇文章主要介紹了C# 實現(xiàn)抓包的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • 一個狀態(tài)機的實現(xiàn)

    一個狀態(tài)機的實現(xiàn)

    本文主要介紹了C#實現(xiàn)一個狀態(tài)機的思路與方法,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • C#獲取CPU編號的方法

    C#獲取CPU編號的方法

    這篇文章主要介紹了C#獲取CPU編號的方法,實例分析了C#獲取硬件信息的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • c#操作sqlserver數(shù)據(jù)庫的簡單示例

    c#操作sqlserver數(shù)據(jù)庫的簡單示例

    這篇文章主要介紹了c#操作sqlserver數(shù)據(jù)庫的簡單示例,需要的朋友可以參考下
    2014-04-04
  • C#基礎(chǔ)語法:方法參數(shù)詳解

    C#基礎(chǔ)語法:方法參數(shù)詳解

    這篇文章主要介紹了C#基礎(chǔ)語法:方法參數(shù)詳解,本文講解了值參數(shù)、引用參數(shù)、輸出參數(shù)、參數(shù)數(shù)組等參數(shù)類型,并分別給出代碼實例,需要的朋友可以參考下
    2015-06-06
  • C#實現(xiàn)多線程編程的簡單案例

    C#實現(xiàn)多線程編程的簡單案例

    這篇文章介紹了C#實現(xiàn)多線程編程的簡單案例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04

最新評論