Search code examples
.netwpftelerik

Set trackball header based on x position


I'm trying to set the header of a wpf telerik trackball. What I'd like to display is "X position: 12.2", with the 12.2 based on where the trackball is currently positioned.

I'm using a SeriesProvider for generating the scatterlines. What I currently have is the seriesProvider:

    <telerik:RadCartesianChart.SeriesProvider>
        <telerik:ChartSeriesProvider Source="{Binding LineOutputModels, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
            <telerik:ScatterSeriesDescriptor
                ItemsSourcePath="DataPoints"
                XValuePath="X"
                YValuePath="Y">
                <telerik:ScatterSeriesDescriptor.Style>
                    <Style TargetType="telerik:ScatterLineSeries">
                        <Setter Property="Stroke" Value="{Binding Color}" />
                        <Setter Property="TrackBallInfoTemplate" Value="{StaticResource trackBallInfoTemplate}" />
                    </Style>
                </telerik:ScatterSeriesDescriptor.Style>
            </telerik:ScatterSeriesDescriptor>
       </telerik:ChartSeriesProvider>
   </telerik:RadCartesianChart.SeriesProvider>

What I wanted to do was something like

    <telerik:RadCartesianChart.TrackBallInfoStyle>
        <Style TargetType="telerik:TrackBallInfoControl">
            <Setter Property="Header" Value="{Binding Trackball.XPosition}" />
        </Style>
    </telerik:RadCartesianChart.TrackBallInfoStyle>

But I couldn't make it work. Do I have to use code behind for this?


Solution

  • The data context of the TrackBallInfoControl is inherited from the DataContext of the parent RadCartesianChart element.

    To achieve your requirement, you can assign x:Name to the ChartTrackBallBehavior and use ElementName binding in order to access the Position property of the behavior.

    <telerik:RadCartesianChart.TrackBallInfoStyle>
        <Style TargetType="telerik:TrackBallInfoControl">
            <Setter Property="Header" Value="{Binding ElementName=trackBallBehavior, Path=Position.X}" />
        </Style>
    </telerik:RadCartesianChart.TrackBallInfoStyle>
    
    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartTrackBallBehavior x:Name="trackBallBehavior"/>
    </telerik:RadCartesianChart.Behaviors>