LiveCharts WPF自定义Tooltip不显示数据 如何实现自定义提示内容?
解决方案
你不需要手动绑定SeriesCollection,LiveCharts已经自动将悬浮位置匹配的所有数据注入到Tooltip的Data属性中,之前的Tooltip失效是因为绑定了不存在的CustomerVm相关属性(你的Series存储的是double类型数值,并非自定义的CustomerVm对象),直接修改Tooltip的XAML代码即可实现你要的效果。
修改后的Tooltip XAML代码:
<UserControl x:Class="LandlordTenantDatabaseWPF.CustomersTooltip" 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" xmlns:wpf="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf" xmlns:local="clr-namespace:LandlordTenantDatabaseWPF" mc:Ignorable="d" d:DesignHeight="100" d:DesignWidth="300" Background="#E4555555" Padding="20 10" BorderThickness="2" BorderBrush="#555555"> <ItemsControl ItemsSource="{Binding Data.Points}" Grid.IsSharedSizeScope="True"> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type wpf:DataPointViewModel}"> <Grid Margin="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <!-- 保留系列颜色标识 --> <Rectangle Grid.Column="0" Stroke="{Binding Series.Stroke}" Fill="{Binding Series.Fill}" Height="15" Width="15"/> <!-- 直接绑定系列名和Y轴数值,按要求格式化 --> <TextBlock Grid.Column="1" VerticalAlignment="Center" Foreground="White" Margin="5 0 0 0"> <Run Text="{Binding Series.Title}"/> <Run Text=" Payment: $"/> <Run Text="{Binding ChartPoint.Y, StringFormat=N2}"/> </TextBlock> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </UserControl>
补充优化建议
你当前的ChartData类中属性变更通知的参数存在错误,属性名需要和公开属性名保持一致,避免后续出现绑定不更新的问题,修改示例如下:
public SeriesCollection SeriesCollection { get { return seriesCollection; } set { if (seriesCollection != value) { seriesCollection = value; // 参数要和公开属性名一致,建议用nameof避免手写错误 OnPropertyChanged(nameof(SeriesCollection)); } } } // 其余Labels、Title、Formatter属性的OnPropertyChanged参数都做对应修改即可
多系列场景下,Tooltip会自动展示当前悬浮位置所有系列的对应数据,每条数据都会显示对应系列的名称和支付金额,完全符合你的需求。
内容的提问来源于stack exchange,提问作者David.Warwick
相关产品推荐
相关产品推荐

