自定义WPF中Slider的Autotooltip模板

Slider控件有一个我比较喜欢的属性"AutoToolTip",可以在拖动的过程中显示当前刻度,然而这个刻度却不支持模板定制,并且就连自定义格式也不行。这就大大的限制了它的使用范围。网上有篇文章Modifying the auto tooltip of a Slider(由于wordpress被和谐了,这个地址是无法访问的)解决了这个问题,可以实现自定义显示格式

代码如下: 

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
/// A Slider which provides a way to modify the 
/// auto tooltip text by using a format string.
/// </summary>

public class FormattedSlider : Slider
ExpandedBlockStart.gifContractedBlock.gif
{
    
private ToolTip _autoToolTip;
    
private string _autoToolTipFormat;

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <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
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _autoToolTipFormat; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set { _autoToolTipFormat = value; }
    }


    
protected override void OnThumbDragStarted(DragStartedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
base.OnThumbDragStarted(e);
        
this.FormatAutoToolTipContent();
    }


    
protected override void OnThumbDragDelta(DragDeltaEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
base.OnThumbDragDelta(e);
        
this.FormatAutoToolTipContent();
    }


    
private void FormatAutoToolTipContent()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (!string.IsNullOrEmpty(this.AutoToolTipFormat))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.AutoToolTip.Content = string.Format(
                
this.AutoToolTipFormat, 
                
this.AutoToolTip.Content);
        }

    }


    
private ToolTip AutoToolTip
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (_autoToolTip == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                FieldInfo field 
= typeof(Slider).GetField(
                    
"_autoToolTip",
                    BindingFlags.NonPublic 
| BindingFlags.Instance);

                _autoToolTip 
= field.GetValue(thisas ToolTip;
            }


            
return _autoToolTip;
        }

    }
    
}

 

使用起来也很简单。
<local:FormattedSlider
     AutoToolTipFormat="{}{0}% used"
    AutoToolTipPlacement="BottomRight" />

其实原理也不复杂,通过反射设置"_autoToolTip"变量,从而实现自定义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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值