【问题标题】:DataContext of DataTemplate in ToolTip Style in WPFWPF中ToolTip样式中DataTemplate的DataContext
【发布时间】:2012-12-19 03:34:38
【问题描述】:

我似乎无法在 ToolTipStyle 样式中为 Grid 找到正确的 DataContext。只会出现一个空白的工具提示。

<Window.Resources>
    <DataTemplate x:Key="ListTemplate">
        <StackPanel>
            <Grid>
                <TextBlock Text="{Binding Path=Name}">
                    <TextBlock.ToolTip>
                        <ToolTip Style="{StaticResource ToolTipStyle}" />
                    </TextBlock.ToolTip>
                </TextBlock>
            </Grid>
        </StackPanel>
    </DataTemplate>

    <Style TargetType="ToolTip" x:Key="ToolTipStyle">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding Path=Description}" />
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Windows.Resources>

<ListBox ItemTemplate="{StaticResource ListTemplate}" />

【问题讨论】:

    标签: xaml styles tooltip datatemplate datacontext


    【解决方案1】:

    工具提示/弹出窗口似乎存在于可视化树之外。我看到很多人绑定到 PlacementTarget 属性以返回。

    DataContext="{绑定路径=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"

    <DataTemplate x:Key="ListTemplate">
        <StackPanel>
            <Grid>
                <TextBlock>
                    <TextBlock.ToolTip>
                        <ToolTip DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" Text="{Binding Path=Name}" Style="{StaticResource ToolTipStyle}" />
                    </TextBlock.ToolTip>
                </TextBlock>
            </Grid>
        </StackPanel>
    </DataTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多