WPF - 简单的相对路径 - FindAncestor

时间:2010-04-05 17:47:53

标签: wpf binding

在下面的XAML中,ToolTip正确绑定到RelativeSource Self。但是,我不能为我的生活弄清楚如何在注释块中获取TextBlock来引用SelectedItem.Description

<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>

我很感激任何建议

谢谢 - 杰里米

2 个答案:

答案 0 :(得分:3)

似乎由于ToolTip没有父级,您需要绑定到放置目标,如下所示:

<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>

希望这对某人有用 杰里米

答案 1 :(得分:1)

self的相对来源是指当前对象,在这种特殊情况下,它将是TextBox本身。您需要具有祖先类型RadComboBoxWithCommand的查找祖先的相对来源。或者,也许更简单一点,就是给组合框一个名称,并在绑定中使用ElementName而不是相对源:

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