2010-04-05 231 views
2

在下面的XAML中,ToolTip正确地绑定到RelativeSource Self。但是,我不能为我的生活工作如何获得在评论块的TextBlock指SelectedItem.DescriptionWPF - 简单相对路径 - FindAncestor

<Controls:RadComboBoxWithCommand x:Name="cmbPacking" 
           Grid.Row="2" 
           Grid.Column="5" 
           ItemsSource="{Binding PackingComboSource}" 
           DisplayMemberPath="DisplayMember" 
           SelectedValuePath="SelectedValue" 
           SelectedValue="{Binding ElementName=dataGrid1, Path=SelectedItem.PackingID}" 
           ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Description}" 
           IsSynchronizedWithCurrentItem="True" 
           Style="{StaticResource comboBox}"> 
<!--     <Controls:RadComboBoxWithCommand.ToolTip>--> 
       <!--      <TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Description}" TextWrapping="Wrap" Width="50"/>--> 
<!--     </Controls:RadComboBoxWithCommand.ToolTip>-->     
</Controls:RadComboBoxWithCommand> 

我将不胜感激任何建议

谢谢 - 杰里米

回答

1

自我的相对来源意味着当前对象,在这种特殊情况下,它将成为TextBox本身。你需要一个祖先类型为RadComboBoxWithCommand的祖先的相对来源。或者,也许有点简单,就是给组合框的名称,并在你的绑定,而不是一个相对源使用的ElementName:

<ComboBox x:Name="cb" ...> 
    <ComboBox.ToolTip> 
     <TextBlock Text="{Binding SelectedItem.Description, ElementName=cb}" .../> 
+0

不幸的是,这并不工作,也不使用FindAncestor有用。但是,经过多一点研究,我发现这篇文章http://blogs.msdn.com/tom_mathews/archive/2006/11/06/binding-a-tooltip-in-xaml.aspx。您需要绑定到PlacementTarge。 – 2010-04-05 21:06:11

3

看来,因为工具提示没有一个家长,你需要绑定到放置目标如下:

<Controls:RadComboBoxWithCommand Grid.Row="2" 
              Grid.Column="5" 
              ItemsSource="{Binding PackingComboSource}" 
              DisplayMemberPath="DisplayMember" 
              SelectedValuePath="SelectedValue" 
              SelectedValue="{Binding ElementName=dataGrid1, Path=SelectedItem.PackingID}" 
              IsSynchronizedWithCurrentItem="True" 
              Style="{StaticResource comboBox}"> 
       <Controls:RadComboBoxWithCommand.ToolTip> 
        <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}"> 
         <TextBlock Text="{Binding SelectedItem.Description}" 
            TextWrapping="Wrap" 
            Width="100" /> 
        </ToolTip> 
       </Controls:RadComboBoxWithCommand.ToolTip> 
      </Controls:RadComboBoxWithCommand> 

希望这是有人 杰里米