wpf - why does this XAML override template not work
Any ideas regarding why the WPF XAML code I have is not working. I'm trying to override the WPFToolkit charting display, and have taken their default XAML and included in my Grid.Resources section as a means of overriding. Specifically I'm wanting to remove the graph markers, but this specific question pertains to clarifying my understanding of XAML by asking why these specific approaches do not work:
a) - I've tried putting Visibility="Hidden in the Grid element but this doesn't seem to work? Why would this be?
b) tried taking out all the lines in the tag, however this doesn't work. Why would this be? Should this not override things. I'm wondering if my whole override template here is really working at all for LineDataPoint? (I do note that the LineSeries override I have in the below code however do work)
XAML is:
<!-- charting:LineSeries -->
<Style TargetType="chartingToolkit:LineSeries">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="PolylineStyle">
<Setter.Value>
<Style TargetType="Polyline">
<Setter Property="StrokeThickness" Value="1" />
</Style>
</Setter.Value>
</Setter>
</Style>
<!-- charting:LineDataPoint -->
<Style TargetType="chartingToolkit:LineDataPoint">
<Setter Property="Background" Value="Orange" />
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Width" Value="2" />
<Setter Property="Height" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:LineDataPoint">
<Grid x:Name="Root" Opacity="0" Visibility="Hidden">
<Ellipse Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" Height="30"/>
<Ellipse RenderTransformOrigin="0.661,0.321">
<Ellipse.Fill>
<RadialGradientBrush GradientOrigin="0.681,0.308">
<GradientStop Color="Green" />
<GradientStop Color="#FFFFFFFF" Offset="1" />
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Ellipse x:Name="SelectionHighlight" Opacity="0" Fill="Red" />
<Ellipse x:Name="MouseOverHighlight" Opacity="0" Fill="White" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
&l开发者_JS百科t;/Style>
</Grid.Resources>
<chartingToolkit:Chart Title="Engine Performance">
<!-- Power curve -->
<chartingToolkit:LineSeries
Title="Power"
ItemsSource="{StaticResource EngineMeasurementCollection}"
IndependentValueBinding="{Binding Speed}"
DependentValueBinding="{Binding Power}">
<!-- Vertical axis for power curve -->
<chartingToolkit:LineSeries.DependentRangeAxis>
<chartingToolkit:LinearAxis
Orientation="Y"
Title="Power (hp)"
Minimum="0"
Maximum="250"
Interval="50"
ShowGridLines="True"/>
</chartingToolkit:LineSeries.DependentRangeAxis>
</chartingToolkit:LineSeries>
<!-- Torque curve -->
<chartingToolkit:LineSeries
Title="Torque"
ItemsSource="{StaticResource EngineMeasurementCollection}"
IndependentValueBinding="{Binding Speed}"
DependentValueBinding="{Binding Torque}">
<!-- Vertical axis for torque curve -->
<chartingToolkit:LineSeries.DependentRangeAxis>
<chartingToolkit:LinearAxis
Orientation="Y"
Title="Torque (lb-ft)"
Minimum="50"
Maximum="300"
Interval="50"/>
</chartingToolkit:LineSeries.DependentRangeAxis>
</chartingToolkit:LineSeries>
<chartingToolkit:Chart.Axes>
<!-- Shared horizontal axis -->
<chartingToolkit:LinearAxis
Orientation="X"
Title="Speed (rpm)"
Interval="1000"
ShowGridLines="True"/>
</chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>
</Grid>
EDIT:
PS. I've boiled it down to the fact the template isn't picked up it seem in the code below - but it should be picked up no? i.e. I have NOT set an x:key against
<Window x:Class="MyInternetUsage.EnginePerformance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:local="clr-namespace:DataVisualizationDemos" xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" Title="EnginePerformance" Height="277" Width="371">
<Grid>
<Grid.Resources>
<local:EngineMeasurementCollection x:Key="EngineMeasurementCollection"/>
<!-- charting:LineDataPoint -->
<Style TargetType="chartingToolkit:LineDataPoint">
<Setter Property="Background" Value="Orange" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:LineDataPoint">
<Grid
Width="30"
Height="30"
Background="{TemplateBinding Background}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<chartingToolkit:Chart Title="Engine Performance">
<!-- Power curve -->
<chartingToolkit:LineSeries
Title="Power"
ItemsSource="{StaticResource EngineMeasurementCollection}"
IndependentValueBinding="{Binding Speed}"
DependentValueBinding="{Binding Power}">
</chartingToolkit:LineSeries>
</chartingToolkit:Chart>
</Grid>
</Window>
thanks
It is possible that the LineDataPoint is not templatable or at least not fully, by virtue of having many of its style properties defined internally.
You can set a default style as you've done, and this works very well for the "lookless" controls that are part of the framework, because they are designed with templating in mind (and so make use of TemplateBinding
s), but if a control, for example, declares internally that its background is purple, you cannot override this with a style.
It is also possible that what you're seeing is not actually the background of the control itself, rather the background of a constituent control within. If the component designed does not "pass through" the property, setting values on the control itself won't affect the internal parts that you see.
Finally, it is possible that Chart
defines a default style for LineDataPoint
. Being in a narrower scope than your grid resource, this would take precedence.
If you have access to the source, you can suss this out; without the source or good documentation of the component, it is just a frustrating trial-and-error guessing game as to what is going on.
精彩评论