如何将数据 table 绑定到工具提示以显示到具有矩形元素的 canvas 上?
How can I bind a data table to a tooltip to show onto a canvas with rectangle elements?
我有一个 canvas 元素,里面有一堆矩形,我想做的是每次将鼠标放在其中一个矩形上时,我想显示数据的值 table 在与 canvas 或矩形无关的工具提示上。
Kinda like this:
这段代码似乎可以工作:
<Canvas Name="canvasceldas_phas" Width="350" HorizontalAlignment="Center" VerticalAlignment="Center" Height="350">
<Canvas.Resources>
<Style TargetType="Rectangle">
<Setter Property="ToolTip" Value="20" />
</Style>
</Canvas.Resources>
</Canvas>
(基本上弹出一个带有“20”的工具提示)
但我想要的是显示“20”的 insead,我希望预加载数据的值 table 显示在与数据属于同一位置的矩形工具提示中 table,例如,我将鼠标悬停在矩形 (0,1) 上,我希望在工具提示上显示数据 table 的元素 (0,1)。 (不知道自己解释清楚没有)
我已经试过了,但似乎不起作用
<Canvas Name="canvasceldas_phas" Width="350" HorizontalAlignment="Center" VerticalAlignment="Center" Height="350">
<Canvas.Resources>
<Style TargetType="Rectangle">
<Setter Property="ToolTip" Value="{Binding datatable}" />
</Style>
</Canvas.Resources>
</Canvas>
抱歉,如果这看起来很明显或者我犯了愚蠢的错误,但我是 wpf 的新手,这让我发疯
有任何想法吗?
提前谢谢你:)
欢迎使用 WPF 和 Stack Overflow。我的解决方案使用 tools/methods 你可能不熟悉,这取决于你的绿色程度,但它们是 WPF 中非常重要的部分,所以如果你还不知道它们,你应该学习它们。
我建议不要使用 Canvas
和一堆手动放置的矩形,而是使用 ItemsControl
。 ItemsControl
获取对象列表并使用 DataTemplate
.
显示每个对象
看看下面的代码:
<ItemsControl Width="500" Height="500">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Height="50" Width="50" BorderThickness="5" BorderBrush="Black" ToolTip="{Binding SomeProperty}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
上面ItemsControl
中的每一项都将显示为Border
控件。所有 Border
都将放在 WrapPanel
中。如果您有 100 件商品,则会生成与您提供的图片相匹配的网格图案。每个 Border
的 DataContext
将是它所代表的项目,因此任何绑定都将从该项目中提取数据。
我的解决方案是像上面那样使用 ItemsControl
,并用 "data table" 中的数据填充它,其中 table 中的每个值都是ItemsControl
。然后您可以将 Boder.ToolTip
绑定回您要显示的项目的 属性。
如果您需要更多说明,请告诉我。
我有一个 canvas 元素,里面有一堆矩形,我想做的是每次将鼠标放在其中一个矩形上时,我想显示数据的值 table 在与 canvas 或矩形无关的工具提示上。
Kinda like this:
这段代码似乎可以工作:
<Canvas Name="canvasceldas_phas" Width="350" HorizontalAlignment="Center" VerticalAlignment="Center" Height="350">
<Canvas.Resources>
<Style TargetType="Rectangle">
<Setter Property="ToolTip" Value="20" />
</Style>
</Canvas.Resources>
</Canvas>
(基本上弹出一个带有“20”的工具提示)
但我想要的是显示“20”的 insead,我希望预加载数据的值 table 显示在与数据属于同一位置的矩形工具提示中 table,例如,我将鼠标悬停在矩形 (0,1) 上,我希望在工具提示上显示数据 table 的元素 (0,1)。 (不知道自己解释清楚没有)
我已经试过了,但似乎不起作用
<Canvas Name="canvasceldas_phas" Width="350" HorizontalAlignment="Center" VerticalAlignment="Center" Height="350">
<Canvas.Resources>
<Style TargetType="Rectangle">
<Setter Property="ToolTip" Value="{Binding datatable}" />
</Style>
</Canvas.Resources>
</Canvas>
抱歉,如果这看起来很明显或者我犯了愚蠢的错误,但我是 wpf 的新手,这让我发疯 有任何想法吗? 提前谢谢你:)
欢迎使用 WPF 和 Stack Overflow。我的解决方案使用 tools/methods 你可能不熟悉,这取决于你的绿色程度,但它们是 WPF 中非常重要的部分,所以如果你还不知道它们,你应该学习它们。
我建议不要使用 Canvas
和一堆手动放置的矩形,而是使用 ItemsControl
。 ItemsControl
获取对象列表并使用 DataTemplate
.
看看下面的代码:
<ItemsControl Width="500" Height="500">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Height="50" Width="50" BorderThickness="5" BorderBrush="Black" ToolTip="{Binding SomeProperty}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
上面ItemsControl
中的每一项都将显示为Border
控件。所有 Border
都将放在 WrapPanel
中。如果您有 100 件商品,则会生成与您提供的图片相匹配的网格图案。每个 Border
的 DataContext
将是它所代表的项目,因此任何绑定都将从该项目中提取数据。
我的解决方案是像上面那样使用 ItemsControl
,并用 "data table" 中的数据填充它,其中 table 中的每个值都是ItemsControl
。然后您可以将 Boder.ToolTip
绑定回您要显示的项目的 属性。
如果您需要更多说明,请告诉我。