简体   繁体   English

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

[英]Unable to set the parent datacontext for datatemplate tooltip

Unable to set the parent datacontext for datatemplate tooltip. 无法设置datatemplate工具提示的父datacontext。

Below is the xaml code. 下面是xaml代码。 Just once combo box and in the combobox datatemplate added textbox. 仅一次组合框,并在组合框datatemplate中添加了文本框。

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>

ViewModel 视图模型

    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";
    }

Can you please help me why DataContext is not set. 您能帮我为什么不设置DataContext吗?

Is it binding issue? 有约束力吗?

if you don't need any special things try not to nest the tooltip: 如果您不需要任何特殊的东西,请尽量不要嵌套工具提示:

<TextBlock ToolTip="{Binding DataContext.ToolTipValue, 
                     RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
           Text="{Binding Length}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM