WPF显示来自多个DataContexts在ItemsControl中的工具提示数据
-
22-07-2019 - |
题
我试图显示由一个需要从概念上不相关的来源中提取数据的ItemsControl
生成的项目的工具提示。例如,假设我有一个项目类,如下所示:
public class Item
{
public string ItemDescription { get; set; }
public string ItemName { get; set; }
}
我可以与工具提示一个ItemsControl内显示项目如下:
<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>
但是说我有能够经由DataContext
的ItemsControl
访问另一属性。有没有办法从工具提示中做到这一点?如,
<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 ItemsControl DataContext}" />
</Grid>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
用于测试窗口我使用的代码如下所示:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<Item> itemList = new List<Item>() {
new Item() { ItemName = "First Item", ItemDescription = "This is the first item." },
new Item() { ItemName = "Second Item", ItemDescription = "This is the second item." }
};
this.Items = itemList;
this.GlobalText = "Something else for the tooltip.";
this.DataContext = this;
}
public string GlobalText { get; private set; }
public List<Item> Items { get; private set; }
}
因此,在这个例子中我想显示GlobalText
属性的值(在现实中,这将是另一个自定义对象)。
要复杂的问题,我实际使用的DataTemplates和显示两个不同类型的ItemsControl中的对象,但任何援助将不胜感激!
解决方案
的头发拉我来,你不能引用另一个DataContext的一个DataTemplate的里面的工具提示的信念一小时后。对于其他绑定完全有可能因为其他海报已被证明。这就是为什么你也不能使用的RelativeSource伎俩。你可以做的是你的产品类和参考实现静态属性的是的:
<Window x:Class="ToolTipSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Name="Root"
xmlns:ToolTipSpike="clr-namespace:ToolTipSpike">
<Grid>
<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="{Binding Source={x:Static ToolTipSpike:Item.GlobalText},
Path=.}"
/>
</Grid>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
using System.Collections.Generic;
using System.Windows;
namespace ToolTipSpike
{
public partial class Window1 : Window
{
public List<Item> Items { get; private set; }
public Window1()
{
InitializeComponent();
var itemList = new List<Item>
{
new Item { ItemName = "First Item", ItemDescription = "This is the first item." },
new Item { ItemName = "Second Item", ItemDescription = "This is the second item." }
};
this.Items = itemList;
this.DataContext = this;
}
}
public class Item
{
static Item()
{
GlobalText = "Additional Text";
}
public static string GlobalText { get; set; }
public string ItemName{ get; set;}
public string ItemDescription{ get; set;}
}
}
其他提示
第二次尝试
确定,相对源绑定不在这种情况下强>工作的。它实际上从数据模板工程,你可以找到在因特网和许多这样的例子。但在这里(你是对的,大卫在您的评论)工具提示是不是在的VisualTree(这是一个属性,而不是控制本身)正确放置一个特殊的野兽,它没有访问到正确的名称范围使用相对结合。
在多一些搜索我发现这篇文章,其描述在此的细节和效果提出了一种BindableToolTip的实现。
这可能是矫枉过正,因为你还有其他的选择 - 就像使用一个静态属性的一类(如Dabblernl的反应)或添加新的实例属性您Item
首先尝试:)
您应与相对源绑定类型协商(在此备忘单示例):
所以你的绑定看起来在某种程度上类似于这样:
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path= GlobalText}
几乎正确Yacoder,并猜测错误的方式有Dabblernl;)
您的思路是正确的,有可能引用您的ItemsControl的DataContext的
您缺少DataContext属性路径:
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.GlobalText}
<强>第二次尝试)强>
HTTP:/ /blogs.msdn.com/tom_mathews/archive/2006/11/06/binding-a-tooltip-in-xaml.aspx
下面是同样的问题的制品。它们可通过本PlacementTarget属性来引用它们的父控制的DataContext:
<ToolTip DataContext=”{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Parent}”>
如果您会将在DataContext在更深的层次,你避免更改项目的DataContext
一个第二建议(Neil和斯密)是,我们可以在结合使用PlacementTarget。这是很好的,因为我实际上是从托管DataControl上的页面继承的DataContext已,这将允许工具提示,以获得回origial控制。正如亚当指出,不过,你必须要知道的父/子结构关闭您的标记:
这是我认为这是概念上更合适做这个视图模型比它在视图反正情况。揭露提示信息视图作为视图模型项目的属性。这让视做它擅长(该项目的呈现属性)和视图模型做什么这是很好的(决定应提交什么信息)。
我有一个非常类似的问题,来到了这个问题寻求答案。最后我想出了在我的情况下工作,可能是有用的人不同的解决方案。
在我的解决方案,我添加了一个属性,它引用父模型的子项,并填充它的时候产生的孩子。在XAML的工具提示,我后来干脆引用从每个元素的父模型的属性和设置的DataContext父模型属性。
我感觉更舒服此溶液比创建在XAML代理元素和引用它们。
使用示例代码这个问题,你会做以下。请注意我没有测试在编译器这种情况下,但这样做的成功实施代码这个解决方案为我自己的情况。
档案:
public class Item
{
public List<Item> Parent { get; set; }
public string ItemDescription { get; set; }
public string ItemName { get; set; }
}
窗口:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<Item> itemList = new List<Item>();
itemList.Add(new Item() { Parent = this, ItemName = "First Item", ItemDescription = "This is the first item." });
itemList.Add(new Item() { Parent = this, ItemName = "Second Item", ItemDescription = "This is the second item." });
this.Items = itemList;
this.GlobalText = "Something else for the tooltip.";
this.DataContext = this;
}
public string GlobalText { get; private set; }
public List<Item> Items { get; private set; }
}
XAML:
<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" DataContext={Binding Parent} Text="{Bind this to aproperty of the parent data model}" />
</Grid>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>