2009-04-27 123 views
2

大家下午好, 我必須使用傳統的Winforms應用程序,但我想開始將其遷移到WPF。它現在沒有工具提示控件,所以我想使用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; 

知道爲什麼這不起作用?也許我需要爲每個控件使用工具提示對象,而不是單個全局工具作爲解決方法?

+0

- 好吧,如果我爲每個控件創建一個工具提示,它就可以工作。這是一種解決方法,但我仍然想知道爲什麼它不更新,如果您更改DataContext屬性。也許工具提示對象設計從來沒有打算以這種方式使用? – Jay 2009-04-27 22:18:54

回答

0

該綁定指定了一個Source,因爲它們不再「關心」DataContext,因此如果源對象上的屬性本身以外的任何其他內容發生更改,則綁定不會更新。