WPF工具提示绑定未更新

时间:2009-04-27 19:07:19

标签: wpf data-binding tooltip

下午好, 我必须使用传统的Winforms应用程序,但我想开始将其迁移到WPF。它现在没有工具提示控件,所以我想使用WPF工具提示对象。

我创建了一个工具提示对象的单个全局实例。我绑定了控件,我的应用程序设置了工具提示的datacontext。我可以手动显示和隐藏工具提示。我第一次将鼠标悬停在一个对象上时,它可以完美地获取绑定数据并且效果很好。当我移动另一个控件时,工具提示datacontext被更改,但显示的数据永远不会重新加载。

我尝试实现属性更改事件,并在我绑定的对象中使用INotifyPropertyChanged接口。看来wpf工具提示没有收听事件。

我尝试将绑定模式设置为Oneway(它只是一个显示工具提示)。

工具提示是以编程方式创建的:

// build the tooltip window.
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Width = 50;
img.Height = 60;

// bind the image
System.Windows.Data.Binding imageBinding = new System.Windows.Data.Binding("PatientImage.Data");
imageBinding.Mode = System.Windows.Data.BindingMode.OneWay;
imageBinding.Source = bed;
img.SetBinding(System.Windows.Controls.Image.SourceProperty, imageBinding);

// wrap image in a border
System.Windows.Controls.Border brdr = new System.Windows.Controls.Border();
brdr.BorderBrush = System.Windows.Media.Brushes.Blue;
brdr.Margin = new System.Windows.Thickness(6);
brdr.Child = img;

System.Windows.Controls.WrapPanel wp = new System.Windows.Controls.WrapPanel();

System.Windows.Controls.TextBlock tb = new System.Windows.Controls.TextBlock();
tb.Background = System.Windows.Media.Brushes.LightBlue;
tb.Foreground = System.Windows.Media.Brushes.Blue;

// bind the text block
System.Windows.Data.Binding textBlockBinding = new System.Windows.Data.Binding("TooltipText");
textBlockBinding.Mode = System.Windows.Data.BindingMode.OneWay;
textBlockBinding.Source = bed;
tb.SetBinding(System.Windows.Controls.TextBlock.TextProperty, textBlockBinding);

wp.Children.Add(brdr);
wp.Children.Add(tb);

bedTooltip = new System.Windows.Controls.ToolTip();
bedTooltip.Content = wp;

知道为什么这不起作用?也许我需要为每个控件使用工具提示对象而不是单个全局控件作为解决方法?

1 个答案:

答案 0 :(得分:0)

绑定指定Source,因为它们不再“关心”DataContext,因此如果源对象上的属性本身以外的任何内容发生更改,则绑定不会更新。

相关问题