首页 新闻 会员 周边 捐助

关于WPF中ToolTip的绑定问题

0
悬赏园豆:100 [待解决问题]

假设在Canvas上画了很多个Button控件,大小各不同,怎样让鼠标指到某个button时,ToolTip显示对应Button的长宽

问题补充:

最好是在样式表中写,不涉及后台代码

stonefish的主页 stonefish | 初学一级 | 园豆:100
提问于:2011-08-30 16:11
< >
分享
所有回答(1)
0

帮你写了一个,可以实现。不知道是不是你想要的。

xaml中 MouseEnter="UserControl_MouseEnter"

<Canvas Width="300" Height="300">
<Button Width="100" Height="30" Canvas.Left="27" Canvas.Top="31">
</Button>
<Button Canvas.Left="162" Canvas.Top="92" Content="Button" Height="23" Name="button1" Width="75"/>
<Canvas.ToolTip>
<StackPanel>
<TextBlock Text="{Binding ButtonWidth}"/>
<TextBlock Text="{Binding ButtonHeight}"/>
</StackPanel>
</Canvas.ToolTip>
</Canvas>
publicpartialclass FindButton : UserControl
{
public FindButton()
{
InitializeComponent();

this.DataContext =this;
}

publicdouble ButtonWidth { get; set; }
publicdouble ButtonHeight { get; set; }


privatevoid UserControl_MouseEnter(object sender, MouseEventArgs e)
{
var canvas
= sender as UserControl;
if (canvas==null)
return;
var point
= e.GetPosition(canvas);
var dp
= canvas.InputHitTest(point) as DependencyObject;
var btn
= dp.FindAncestor<Button>();
if (btn==null) return;
ButtonWidth
= btn.Width;
ButtonHeight
= btn.Height;
}
}
Lee's Blog | 园豆:530 (小虾三级) | 2011-08-31 09:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册