相关疑难解决方法(0)

WPF在ItemsControl的ToolTip中显示来自多个DataContexts的数据

我正在尝试显示由ItemsControl需要从概念上不相关的源提取数据的项目生成的工具提示.例如,假设我有一个Item类,如下所示:

public class Item
{
    public string ItemDescription { get; set; }
    public string ItemName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我可以使用工具提示在ItemsControl中显示Item,如下所示:

<ItemsControl x:Name="itemsControl" ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ItemName}">
                <TextBlock.ToolTip>
                    <ToolTip>
                        <TextBlock Text="{Binding ItemDescription}" />
                    </ToolTip>
                </TextBlock.ToolTip>
            </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

但说我有一个可以通过访问其他属性DataContextItemsControl.有没有办法从工具提示中做到这一点?例如,

<ItemsControl x:Name="itemsControl" ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ItemName}">
                <TextBlock.ToolTip>
                    <ToolTip>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding ItemDescription}" />
                            <TextBlock Grid.Row="1" Text="{Bind this to another property of the …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf datacontext tooltip itemscontrol

7
推荐指数
1
解决办法
6862
查看次数

标签 统计

data-binding ×1

datacontext ×1

itemscontrol ×1

tooltip ×1

wpf ×1