Silverlight 2 RTW中ToolTipService.ToolTip不继承父节点的DataContext的问题

   在Silverlight2 RTW中,利用ToolTipService.ToolTip可以实现ToolTip(提示)效果,例如:

< Button  Width ="100"  Height ="40"  Content ="Button"  ToolTipService.ToolTip ="这是提示信息"   />

   显示如图:

  我们还可以自定义提示信息的显示样式,例如改变字体或者显示复杂的图形,等等。例如:
         < Button  Width ="100"  Height ="40"  Content ="Button" >
            
< ToolTipService.ToolTip >
                
< Grid >
                    
< Ellipse  Width ="150"  Height ="50"  Fill ="Beige" ></ Ellipse >
                    
< TextBlock  Text ="这是提示信息"  Foreground ="Red"   />
                
</ Grid >
            
</ ToolTipService.ToolTip >
        
</ Button >
  显示如图:
   对于提示信息,同样可以使用 Binding (特别是在ControlTemplate里常常使用Binding,例如ListBoxItem)。例如:
         < Grid  DataContext =" {StaticResource MyUser} " >
            
< Button  Width ="100"  Height ="40"  Content ="Button"  ToolTipService.ToolTip =" {Binding UserName} "   />
        
</ Grid >
    但下面的这种绑定方式确不能正常显示提示信息:
         < Grid  DataContext =" {StaticResource MyUser} " >
            
< Button  Width ="100"  Height ="40"  Content ="Button" >
                
< ToolTipService.ToolTip >
                    
< TextBlock x:Name="txt Text =" {Binding UserName} "   />
                
</ ToolTipService.ToolTip >
            
</ Button >
        
</ Grid >
   
   这是因为在Silverlight 2RTW里,ToolTipService.ToolTip没有继承上层元素的DataContext。如果我们显示指定ToolTipService.ToolTip的DataContext,这种方式同样可以工作。但显示指定ToolTip的DataContext在有的场合是有点别扭,特别是在ControlTemplate里更是有一定的困难。
   为了使ToolTip能够利用父级的DataContext进行绑定,在 http://silverlight.net/forums/p/14241/46745.aspx#46745讨论了在Beta 1下修改ToolTip原代码的方法。目前我还没有找到Silverlight 2 RTW下对应的Controls完整示例代码,为此,我们可以用一个变通的方式来绕过这个问题。既然直接在 ToolTipService.ToolTip =" {Binding UserName} "  里能够成功绑定,为了实现复杂的提示信息,我们可以借助Converter来实现:
     < UserControl.Resources >
        
< local:Converter  x:Name ="myConverter"   />
    
</ UserControl.Resources >

    
< Grid  DataContext =" {StaticResource MyUser} " >
        
< Button  Width ="100"  Height ="40"  Content ="Button"  ToolTipService.ToolTip =" {Binding UserName,Converter={StaticResource myConverter}} "   />
    
</ Grid >
    关键的部分我们在Converter里实现:
     public   class  Converter : IValueConverter
    {
        
#region  IValueConverter Members

        
public   object  Convert( object  value, Type targetType,  object  parameter, System.Globalization.CultureInfo culture)
        {
            
if  (value  !=   null )
            {
                var s 
=  value.ToString();
                TextBlock block 
=   new  TextBlock();
                block.Text 
=  s;
                block.Style 
=  (Style)Application.Current.Resources[ " MyTextBlockStyle " ];
                
return  block;
            }
            
return   null ;
        }

        
public   object  ConvertBack( object  value, Type targetType,  object  parameter, System.Globalization.CultureInfo culture)
        {
            
throw   new  NotImplementedException();
        }






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值