无法为数据模板工具提示设置父数据上下文

无法为数据模板工具提示设置父数据上下文。


下面是 xaml 代码。只需一次组合框并在组合框数据模板中添加文本框。


Xaml


<UserControl x:Class="WpfApplication1.UserControl1"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             mc:Ignorable="d" 

             Name="UC"

             d:DesignHeight="50" d:DesignWidth="200">

    <Grid>

        <ComboBox Width="200" Height="50" ItemsSource="{Binding Coll}">

            <ComboBox.ItemTemplate>

                <DataTemplate>

                    <TextBlock Text="{Binding Length}">

                        <TextBlock.ToolTip>

                            <ToolTip Content="{Binding Path=DataContext.ToolTipValue, 

                                                       RelativeSource={RelativeSource FindAncestor, 

                                                       AncestorType={x:Type UserControl}}}"/>

                        </TextBlock.ToolTip>

                    </TextBlock>

                </DataTemplate>

            </ComboBox.ItemTemplate>

        </ComboBox>

    </Grid>

</UserControl>

视图模型


    private List<string> _coll;


    public List<string> Coll

    {

        get { return _coll; }

        set { _coll = value; OnPropertyChanged(); }

    }


    private string _ToolTipValue;


    public string ToolTipValue

    {

        get { return _ToolTipValue; }

        set { _ToolTipValue = value; OnPropertyChanged(); }

    }


    public ViewModel()

    {

        _coll = new List<string>(){ "1", "2", "3"};

        _ToolTipValue = "Demo";

    }

你能帮我为什么没有设置 DataContext 吗?


是绑定问题吗?


料青山看我应如是
浏览 80回答 1
1回答

郎朗坤

如果你不需要任何特殊的东西尽量不要嵌套工具提示:<TextBlock ToolTip="{Binding DataContext.ToolTipValue,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text="{Binding Length}"/>
打开App,查看更多内容
随时随地看视频慕课网APP