2

I have custom annotation in separate xaml file and I want to use brushes from selected chart theme. I have two points:

  1. I’m trying to access to TextAnnotationForeground Brush as follow (using example from SciChart forum here):

    <s:CustomAnnotation x:Class="Charts.SciCharts.MarkerAnnotation"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
                        VerticalAnchorPoint="Top" HorizontalAnchorPoint="Center" Margin="0">
        <s:CustomAnnotation.Template>
            <ControlTemplate TargetType="s:CustomAnnotation">
                <Border>
                    <Viewbox>
                        <StackPanel>
                            <Path Data="m 4 14 4 0 0 -8 3 0 -5 -5 -5 5 3 0 z" Fill="#571CB61C"
                                  Stroke="#FF00B400" StrokeThickness="1" HorizontalAlignment="Center"/>
                            <Border Margin="5" Padding="5 0 5 2" BorderThickness="1" BorderBrush="#FF00B400"
                                    Background="#571CB61C" CornerRadius="5" HorizontalAlignment="Center">
                                <TextBlock FontSize="12" Text="{Binding Label}"  Foreground="{s:ThemeBinding TextAnnotationForeground}"/>
                            </Border>
                        </StackPanel>
                    </Viewbox>
                </Border>
            </ControlTemplate>
        </s:CustomAnnotation.Template>
    </s:CustomAnnotation>
    

And I’ve got error both in design-time and run-time: Inner exception: Not a dependency object

How could I make it?

  1. I need to define one more brush for one of my custom annotation, and this brush should changed according to selected theme. So could I define this brush in custom theme ResourceDictionary and use it the same as in point 1?
Aduciicba
  • 21
  • 4
  • No suggestions?? I've solved my problem with getting brushes and colors from code-behind + binding, but it's ugly :( There should be the way to do it from xaml. – Aduciicba Jun 21 '18 at 09:36

1 Answers1

0

SciChart's ThemeBinding MarkupExtension is designed to get the current theme applied to the DependencyObject and get the resource keyed by the string name provided, e.g.

 <TextBlock Foreground="{s:ThemeBinding TickTextBrush}"/> 

means

Get the ThemeManager.Theme value applied to this Textblock or inherited from above, and then get the resource called TickTextBrush from that theme and apply it to TextBlock.Foreground

There is a list of Themable keys in SciChart WPF's ThemeColorProvider here.

So you could use our ThemeBinding directly. However, if you just want one value out of a dictionary you would be better off doing it this way:

<Grid>
    <Grid.Resources> 
        <ResourceDictionary>
            <!-- Merged Dictionary is required for BasedOn attribute -->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SciChart.Charting;component/Themes/ExpressionDark.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

        <SolidColorBrush x:Key="ColorBrush"  Color="StaticResource MountainLineColor"/>
    </Grid.Resources>


 </Grid>

If ... that's what you wanted to do (get a brush out of a theme and use somewhere else in your app).

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • Thank you for your answer. As I said, I need to bind to the selected theme, so I need ThemeBinding, as you say. And I've written an example how I've tried to use this. I get an exception when I'm trying to use ThemeBinding, it doesn't work. – Aduciicba Jun 29 '18 at 22:59
  • My example is the same as yours: I tried to bind foreground of my custom annotation to TextAnnotationForeground with ThemeBinding, and it occurs an exception. – Aduciicba Jun 29 '18 at 23:01
  • Well the exception message says "Not a DependencyObject" and thats because you've applied ThemeBinding to a SolidColorBrush - which is not a dependencyobject. – Dr. Andrew Burnett-Thompson Jun 30 '18 at 16:14
  • Sorry, maybe my second example isn't correct in terms of applying ThemeBinding in Color for SolidColorBrush. But in my first example, I've used ThemeBinding for Foreground for TextBlock in CustomAnnotation, and I've got the same error about not a dependency object. I don't understand what the difference between your example with TextBlock and my using for CustomAnnotation. What I've missed? – Aduciicba Jul 01 '18 at 18:08
  • I've extended my main using example and removed incorrect using with SolidColorBrush – Aduciicba Jul 01 '18 at 18:24