我有一个WPF图表,我动态绑定BarSeries。不过,我想让BarSeries显示3条信息。我希望在任何给定数据点的工具提示中显示第三条信息。
有没有办法动态绑定我的bareries中给定数据点的工具提示的值/内容?
XAML:
<UserControl.Resources>
<Style
x:Key="SimpleDataPointStyle"
BasedOn="{StaticResource {x:Type chartingToolkit:BarDataPoint}}"
TargetType="{x:Type chartingToolkit:BarDataPoint}">
<Setter Property="Width" Value="20"/>
<Setter Property="Height" Value="20"/>
<Setter x:Name="DataPointToolTip" Property="ToolTip" Value="{Binding Path=Event_Description}"/>
</Style>
</UserControl.Resources>
<Grid x:Name="MetricsPanel" Width="904" Height="376" HorizontalAlignment="Left" VerticalAlignment="Top">
<chartingToolkit:Chart x:Name="MetricChart" HorizontalAlignment="Left" Width="464" Height="352" VerticalAlignment="Top">
<chartingToolkit:Chart.Series>
<chartingToolkit:BarSeries x:Name="MainSeries"
Title="Contribution to Risk and Errors (%)"
IndependentValueBinding="{Binding}"
DependentValueBinding="{Binding}">
<chartingToolkit:BarSeries.DataPointStyle>
<Style
BasedOn="{StaticResource SimpleDataPointStyle}"
TargetType="{x:Type chartingToolkit:BarDataPoint}">
</Style>
</chartingToolkit:BarSeries.DataPointStyle>
</chartingToolkit:BarSeries>
</chartingToolkit:Chart.Series>
</chartingToolkit:Chart>
代码隐藏
highestWeightedEvents = BuildHighestWeightedEventsTable();
MainSeries.DependentValueBinding = new Binding("Cutset_Frequency");
MainSeries.IndependentValueBinding = new Binding("Event_Number");
MainSeries.ItemsSource = highestWeightedEvents.DefaultView;
Style dataPointStyle = (Style)Resources["SimpleDataPointStyle"];
MainSeries.DataPointStyle = dataPointStyle;
如何在代码后面指定工具提示的绑定?
提前感谢...
答案 0 :(得分:1)
我昨晚通过完全重新定义BarDataPoint模板解决了我自己的问题。
样式XAML:
<Style x:Key="NewDataPointStyle" TargetType="chartingToolkit:BarDataPoint">
<Setter Property="Background" Value="SteelBlue" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="BorderBrush" Value="#FF686868" />
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:BarDataPoint">
<Grid x:Name="Root" Opacity="1">
<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="{Binding Path=Event_Description}" />
</ToolTipService.ToolTip>
<StackPanel Orientation="Horizontal"
Background="{TemplateBinding Background}">
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
图表XAML:
<chartingToolkit:Chart x:Name="MetricChart" HorizontalAlignment="Left" Width="464" Height="352" VerticalAlignment="Top">
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Interval="10" Maximum="100" x:Name="XAxis" Orientation="X" Title="Percent Contribution (%)"/>
<chartingToolkit:CategoryAxis Title="Event Number" Orientation="Y"/>
</chartingToolkit:Chart.Axes>
<chartingToolkit:Chart.Series>
<chartingToolkit:BarSeries x:Name="MainSeries"
Title="Contribution to Risk and Errors (%)"
IndependentValueBinding="{Binding}"
DependentValueBinding="{Binding}" BorderThickness="0" BorderBrush="Black" Background="Black" OpacityMask="Black">
<chartingToolkit:BarSeries.DataPointStyle>
<Style
BasedOn="{StaticResource NewDataPointStyle}"
TargetType="{x:Type chartingToolkit:BarDataPoint}">
</Style>
</chartingToolkit:BarSeries.DataPointStyle>
</chartingToolkit:BarSeries>
</chartingToolkit:Chart.Series>
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0"/>
<Setter Property="Height" Value="0"/>
</Style>
</chartingToolkit:Chart.LegendStyle>
</chartingToolkit:Chart>
代码隐藏:
//highestWeightedEvents is a DataTable
highestWeightedEvents = BuildHighestWeightedEventsTable();
MainSeries.DependentValueBinding = new Binding("Cutset_Frequency");
MainSeries.IndependentValueBinding = new Binding("Event_Number");
MainSeries.ItemsSource = highestWeightedEvents.DefaultView;
由于我在代码隐藏中定义DataSeries.ItemsSource,因此ToolTip绑定知道要查找的位置。我希望其他人可以从中受益,因为我花了一整天时间搞清楚这一点:D
答案 1 :(得分:0)
在后面的代码中,您无法动态编辑样式,因为样式在加载时会被密封。因此,您可以从在xaml中创建的样式创建新样式并添加工具提示绑定。像
这样的东西//your code behind
Style newDataPointStyle = (Style)Resources["SimpleDataPointStyle"];
newDataPointStyle.Setters.Add(
new Setter
{
Property = "ToolTip",
Value = new Binding("YourBinding")
});
MainSeries.DataPointStyle = newDataPointStyle;