【问题标题】:WPF - simple relative path - FindAncestorWPF - 简单的相对路径 - FindAncestor
【发布时间】:2010-04-05 17:47:53
【问题描述】:

在下面的 XAML 中,工具提示正确绑定到 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>

如果有任何建议,我将不胜感激

谢谢 - 杰里米

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    好像因为ToolTip没有父级,所以需要绑定到placement target,如下:

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

    希望这对某人有用 杰里米

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-29
      • 2014-07-25
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多