开发者

WPF - simple relative path - FindAncestor

开发者 https://www.devze.com 2022-12-26 06:27 出处:网络
In the XAML below the ToolTip correctly binds to RelativeSource Self. However, I can\'t for the life of me work out how to get the TextBlock in the commented block to refer to SelectedItem.Description

In the XAML below the ToolTip correctly binds to RelativeSource Self. However, I can't for the life of me work out how to get the TextBlock in the commented block to refer to SelectedItem.Description

<Controls:RadComboBoxWithC开发者_C百科ommand 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>

I would appreciate any suggestions

Thanks - Jeremy


It seems that since ToolTip does not have a parent, you need to bind to the placement target as below:

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

Hope this is useful for someone Jeremy


A relative source of self means the current object, which would be the TextBox itself in this particular case. You want a relative source of find ancestor with an ancestor type of RadComboBoxWithCommand. Alternatively, and perhaps a little simpler, is to give the combo box a name and use ElementName in your binding instead of a relative source:

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

精彩评论

暂无评论...
验证码 换一张
取 消