gpt4 book ai didi

c# - 使用 WPF 图表获取自定义工具提示?

转载 作者:行者123 更新时间:2023-11-30 22:34:29 24 4
gpt4 key购买 nike

我使用以下方法创建了一个 ScatterSeries 图表:

    public ScatterPlot()
{
InitializeComponent();


ScatterSeries a = new ScatterSeries();
a.Title = "Series A";

a.IndependentValuePath = "Key";
a.DependentValuePath = "Value";

scatterplot.Series.Add(a);

((ScatterSeries)scatterplot.Series[0]).ItemsSource = new KeyValuePair<DateTime, int>[]
{
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(1), 0),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(2), 150),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(3), 150),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(4), 0),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(5), 0),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(8), 130),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(9), 130),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(10), 0),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(11), 0),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(15), 225),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(16), 225),
new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(17), 0)
};

}

我有一本字典,其中每个数据点都映射到一个单独的文本标签。我想知道是否可以建立一个绑定(bind),以便当我将鼠标移到一个点上时,我得到文本标签而不是实际数字?有关如何执行此操作的任何建议?

最佳答案

设置DataPoint样式...

    <charting:ScatterSeries DependentValuePath="Value"
IndependentValuePath="Key">
<charting:DataPointSeries.DataPointStyle>
<Style TargetType="{x:Type charting:DataPoint}">
<EventSetter Event="MouseEnter" Handler="DataPoint_MouseEnter" />
<!--Style.Triggers>
<Trigger Property="IsMouseDirectlyOver" Value="True">
<Setter Property="ToolTip" Value="Hi There!"/>
</Trigger>
</Style.Triggers--> <!-- This doesnt work!-->
</Style>
</charting:DataPointSeries.DataPointStyle>
<charting:DataPointSeries.ItemsSource>
<Binding BindsDirectlyToSource="True"/>
</charting:DataPointSeries.ItemsSource>
</charting:ScatterSeries>

但是当我测试时,基于触发器的工具提示 setter 没有工作。事件 setter 虽然有效。因此,最快的解决方法可能是使用该事件 setter 并从处理程序中的代码后面设置 TooltipService.ToolTip

但如果您使用的是 MVVM,那么您可以使用一些附加行为来处理此事件并显式设置工具提示。

您可以使用上述样式覆盖 ScatterDataPoint 模板,并将模板设置为此 NewTemplate ...

请注意,在下面的 ControlTemplate 中,我已将 ToolTipService.ToolTip 设置为 DataPoint 的工具提示。所以你将不得不使用上面的样式来设置 TemplateToolTip ...

 <!--  charting:ScatterDataPoint  -->

<Style x:Key="NewTemplate" TargetType="charting:ScatterDataPoint">
<Setter Property="Background" Value="Orange" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="BorderBrush" Value="#FF686868" />
<Setter Property="Width" Value="8" />
<Setter Property="Height" Value="8" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="charting:ScatterDataPoint">
<Grid x:Name="Root" Opacity="0">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="MouseOverHighlight" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.3" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="SelectionHighlight" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.185" />
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="SelectionHighlight" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF8A8A8A" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="RevealStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.5" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Shown">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Root" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Hidden">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ToolTipService.ToolTip>
<ContentControl Content="{TemplateBinding ToolTip}" />
</ToolTipService.ToolTip>
<Path Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" Stretch="Fill" StrokeThickness="1" StrokeLineJoin="Round" Data="F1 M 0,50L 50,0 100,50 50,100L 0,50 Z " />
<Path x:Name="MouseOverHighlight" Stretch="Fill" StrokeThickness="1" StrokeLineJoin="Round" Stroke="{x:Null}" Data="F1 M 0,50L 50,0 100,50 50,100L 0,50 Z " Opacity="0" Fill="#FFFFFFFF" />
<Path x:Name="SelectionHighlight" Stretch="Fill" StrokeThickness="1" StrokeLineJoin="Round" Stroke="{x:Null}" Data="F1 M 0,50L 50,0 100,50 50,100L 0,50 Z " Fill="#FF959595" Opacity="0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

关于c# - 使用 WPF 图表获取自定义工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7802939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com