簡體   English   中英

無法為datatemplate工具提示設置父數據上下文

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

無法設置datatemplate工具提示的父datacontext。

下面是xaml代碼。 僅一次組合框,並在組合框datatemplate中添加了文本框。

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

有約束力嗎?

如果您不需要任何特殊的東西,請盡量不要嵌套工具提示:

<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