Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

wpf graph annotation color

Solved!
Go to solution

How do I change color of the point annotation label? I've found only properties for the shape stroke and fill.

0 Kudos
Message 1 of 7
(6,243 Views)

Similar to Buttons and other ContentControls, you can customize the Label of an Annotation through the LabelTemplate property:


    <ni:PointAnnotation Label="Annotaiton Label">
        <ni:PointAnnotation.LabelTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" Foreground="Green" />
            </DataTemplate>
        </ni:PointAnnotation.LabelTemplate>
    </ni:PointAnnotation>

~ Paul H
0 Kudos
Message 2 of 7
(6,232 Views)

Ah, I see. Would you mind to write it down in code? I need to change it dynamically.

0 Kudos
Message 3 of 7
(6,229 Views)
Solution
Accepted by topic author eugenem

In that case, you want to use a model object for the label and bind the foreground in the data template as well as the text. As a simple example, here is how you can use an anonymous object as the label:


    <ni:PointAnnotation x:Name="pointAnnotaiton">
        <ni:PointAnnotation.LabelTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Text}" Foreground="{Binding Foreground}" />
            </DataTemplate>
        </ni:PointAnnotation.LabelTemplate>
    </ni:PointAnnotation>

    pointAnnotation.Label = new { Text = "Label", Foreground = Brushes.Green };


Normally, you would use a bindable object for the model, in which case you can change the foreground by updating just that property on the model. For this example, to update the foreground you create a new { Text, Foreground } anonymous object (which is a bit easier to show in a forum post :).

~ Paul H
0 Kudos
Message 4 of 7
(6,225 Views)

If I do 

 

Label = new { Text = "annotation", Foreground = Brushes.Green }

 then I see "Text = "annotation", Foreground = Brushes.Green" as a label

0 Kudos
Message 5 of 7
(6,221 Views)

That looks like the ToString value of the object. Did you setup the LabelTemplate with the bindings shown in the previous XAML snippet?


If you need to assign the LabelTemplate from code as well, you can define it as a resource in the Resources for your page or application.

~ Paul H
0 Kudos
Message 6 of 7
(6,218 Views)

thanks!

0 Kudos
Message 7 of 7
(6,201 Views)