2010-04-05 42 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> 

希望這是有人 傑里米