2010-03-09 70 views
2

我尝试在Silverlight 3中将元素绑定使用到ToolTipService.ToolTip中ComboBox的SelectedItem中。 此代码的工作:元素绑定Silverlight 3

<ComboBox x:Name="cboSource" DisplayMemberPath="Name" ToolTipService.ToolTip="{Binding ElementName=cboSource, Path=SelectedItem.Name}" Width="180" /> 

但是这个代码不:

<ComboBox x:Name="cboSource" DisplayMemberPath="Name" Width="180" > 
    <ToolTipService.ToolTip> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding ElementName=cboSource, Path=SelectedItem.Code}" Margin="0,0,5,0"/> 
      <TextBlock Text="-" Margin="0,0,5,0"/> 
      <TextBlock Text="{Binding ElementName=cboSource, Path=SelectedItem.Name}"/> 
     </StackPanel> 
    </ToolTipService.ToolTip> 
</ComboBox> 

名称和代码都在cboSource.ItemsSource项的属性。 在第一个代码中,Name在组合工具提示中正确显示,但在第二个代码中,tooltip为“ - ”。 任何想法?

+0

你得到你的VS输出绑定错误? – Mark 2010-03-09 10:12:32

+0

不,输出 – HolaJan 2010-03-09 10:15:48

+1

没有绑定错误,那么为什么你使用'ToolTipService.ToolTip'对象?你可以尝试使用''而不是? http://msdn.microsoft.com/en-us/library/system.windows.controls.tooltip.aspx – Mark 2010-03-09 10:18:58

回答

1

啊......有趣的工具提示。

ToolTipService实际上是“根”在树的底部(如果你有Mole,你可以仔细检查以验证这一点) - 因此,它不会从DataContext传播到父元素。

我已经做了hacky的事情来解决这个问题,但他们都归结为“编写一个接受DataContext并将其转发给附加元素的附加属性”。

祝你好运 - 这件事已经刺痛了我几次。 :)

哦,找到了你一个链接:下面的类

 <ComboBox x:Name="cboSource" DisplayMemberPath="Name" Width="180"> 
     <local:DataBindingTooltip.TooltipDataContext> 
      <Binding ElementName="cboSource"/> 
     </local:DataBindingTooltip.TooltipDataContext> 
     <local:DataBindingTooltip.Tooltip> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding Path=SelectedItem.Code}" Margin="0,0,5,0"/> 
        <TextBlock Text="-" Margin="0,0,5,0"/> 
        <TextBlock Text="{Binding Path=SelectedItem.Name}"/> 
       </StackPanel> 
     </local:DataBindingTooltip.Tooltip> 
    </ComboBox> 

http://www.codeproject.com/Articles/36078/Silverlight-2-0-How-to-use-a-DataBinding-with-the-ToolTipService.aspx

编辑:尝试了这一点

public class DataBindingTooltip 
{ 
    public static readonly DependencyProperty TooltipDataContextProperty = 
     DependencyProperty.RegisterAttached(
      "TooltipDataContext", 
      typeof (object), 
      typeof (DataBindingTooltip), 
      null); 

    public static readonly DependencyProperty TooltipProperty = 
     DependencyProperty.RegisterAttached(
      "Tooltip", 
      typeof(object), 
      typeof(DataBindingTooltip), 
      new PropertyMetadata(TooltipChanged)); 

    public static void SetTooltip(DependencyObject d, object value) 
    { 
     d.SetValue(TooltipProperty, value); 
    } 
    public static object GetTooltip(DependencyObject d) 
    { 
     return d.GetValue(TooltipProperty); 
    } 

    public static void SetTooltipDataContext(DependencyObject d, object value) 
    { 
     d.SetValue(TooltipDataContextProperty, value); 
    } 
    public static object GetTooltipDataContext(DependencyObject d) 
    { 
     return d.GetValue(TooltipDataContextProperty); 
    } 

    private static void TooltipChanged(DependencyObject sender, 
     DependencyPropertyChangedEventArgs e) 
    { 
     if (sender is FrameworkElement) 
     { 
      var element = sender as FrameworkElement; 
      element.Loaded += ElementLoaded; 
     } 

    } 

    static void ElementLoaded(object sender, RoutedEventArgs e) 
    { 
     if (sender is FrameworkElement) 
     { 
      var element = sender as FrameworkElement; 
      element.Loaded -= ElementLoaded; 

      var tooltip = element.GetValue(TooltipProperty) as DependencyObject; 
      if (tooltip != null) 
      { 
       if (GetTooltipDataContext(element) != null) 
       { 
        tooltip.SetValue(FrameworkElement.DataContextProperty, 
            element.GetValue(TooltipDataContextProperty)); 
       } 
       else 
       { 
        tooltip.SetValue(FrameworkElement.DataContextProperty, 
            element.GetValue(FrameworkElement.DataContextProperty)); 
       } 
      } 
      ToolTipService.SetToolTip(element, tooltip); 
     } 
    } 
} 
+0

这并不能解决这个问题。没有绑定到DataContext,使用Element to Element绑定。 – HolaJan 2010-03-11 12:23:21

+0

检查我的编辑 - 它仍然可以附加属性 – JerKimball 2010-03-11 16:32:10

1

一个非常简单的方法可能是在源对象中定义附加属性

每当用户将鼠标悬停在控件上,串联的字符串将显示为一个很好的简单工具提示。

这样的:

using System... 
.... 


public Class Employee 
{ 
    public string Forenames {get;set;} 
    public string Surname {get;set;} 
    public string Address {get;set;} 
    private string tooltip; 
    public string Tooltip 
    { 
    get{return tooltip;} 
    set 
    { 
    value=Forenames + " " + Surname + "," Address ; 
    } 
} 
//... other methods to follow 
} 


XAML MyPage.cs code has following 

public partial Class MyPage : Page 
{ 
Public List<Employee> Employees{get;set;} 
public MyPage() 
{ 
InitiazeComponents(); 
Employees = new List<Employee>(); // initialise 
Employees=GetEmployees(); 
} 

public List<Employee> GetEmployees(){ 
.. 
Write code that ..returns 
.. 
} 
.. other code to follow.. 
} 

现在MyPage.xaml

... 

<ComboBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="8,4,0,0" Name="cboCostCentreInvestor" ItemsSource="{Binding Employees}" ToolTipService.ToolTip="{Binding ElementName=cboCostCentreInvestor,Path=SelectedItem.Tooltip}"> 
         <ComboBox.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal" Style="{StaticResource stackPanelComboboxItemStyle}"> 
            <TextBlock Text="{Binding Forenames}" Style="{StaticResource textBlockComboboxItem}" /> 
            <TextBlock Text="{Binding Surname}" Style="{StaticResource textBlockComboboxItem}" />         
           </StackPanel> 
          </DataTemplate> 
         </ComboBox.ItemTemplate> 


       </ComboBox>