Pre loader

Accessing XAML LegendTemplate in ViewModel

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

0
0

I have a DataTemplate defined in my xaml file that I’d like to access from my ViewModel that I use to manage the layout and look of the chart legend

Here is the DataTemplate I have defined:

<DataTemplate x:Key="CustomLegendTemplate"
                  DataType="s:SeriesInfo">
        <Grid HorizontalAlignment="Right"
              VerticalAlignment="Top"
              Background="Transparent">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <!--  Series icon, binds to SeriesInfo.Stroke  -->
            <Ellipse Grid.Column="0"
                     Width="7"
                     Height="7"
                     Margin="2 2 8 0"
                     HorizontalAlignment="Right"
                     VerticalAlignment="Top"
                     Fill="{Binding Stroke,
                                    Converter={StaticResource ColorToBrushConverter}}" />

              <!--Series name (SeriesInfo.SeriesName)-->  
            <TextBlock Grid.Column="1"
                           Width="15"
                           HorizontalAlignment="Right"
                           FontSize="10"
                           Text="{Binding SeriesName}" />

            <!--  Show or hide the 'Visibility Checkboxes'  -->
            <Grid Grid.Column="1"
                  Background="Transparent"
                  Visibility="{Binding DataContext.ShowVisibilityCheckboxes,
                                       ElementName=PART_LegendItems,
                                       Converter={StaticResource BooleanToVisibilityConverter}}">

                <!--  Binds to SeriesInfo.IsVisible to show and hide the series  -->
                <CheckBox Width="16"
                          Height="16"
                          HorizontalAlignment="Right"
                          VerticalAlignment="Top"
                          FontSize="10"
                          Foreground="{Binding Stroke,
                                               Converter={StaticResource ColorToBrushConverter}}"
                          IsChecked="{Binding IsVisible,
                                              Mode=TwoWay}">
                    <CheckBox.LayoutTransform>
                        <ScaleTransform ScaleX="0.7" ScaleY="0.7" />
                    </CheckBox.LayoutTransform>
                </CheckBox>
            </Grid>
        </Grid>
    </DataTemplate>

Now I’d like to access this template from my ViewModel to help manipulate my Legend the ViewModel code I have :

ModifierGroup legendInfo = new ModifierGroup(new LegendModifier()
            {
                ShowLegend = true,
                Orientation = Orientation.Vertical,
                Margin = new Thickness(0, 0, 0, 0),
                LegendPlacement = LegendPlacement.Inside,
                GetLegendDataFor = SourceMode.AllSeries,
                ShowVisibilityCheckboxes = true,
                FontSize = 8,
                Background = (Brush)AxisColor.ConvertFromString("Transparent"),
                ShowSeriesMarkers = true
            });

Then I add this to a ChartModifier = new ModifierGroup(XAxisDrag, YAxisDrag, ZoomExtents, RubberBandZoom, PanModifier, Rollover, legendInfo);

It is all working beautifully, but how can I reach for the xaml DataTemplate I have defined ?

Thank you for the excellent support

Version
6.5.1.26063
  • You must to post comments
0
0

Hi Anders

It’s considered bad practice to access Xaml DataTemplates in a ViewModel in the MVVM pattern and should be avoided.

I’d ask ‘why do you want to do this?’ aka what problem are you trying to solve?

If you must create & access a Xaml template in Viewmodel, the best way to do this is as follows:

Accessing a resource via codebehind in WPF

  1. Declare your XAML Template “CustomLegendTemplate” in a resource dictionary linked to App.xaml
  2. You can now access it as follows:

    var dataTemplate = Application.Current.Resources["CustomLegendTemplate"] as DataTemplate;
    

Let me know if this helps

Best regards
Andrew

  • You must to post comments
0
0

Hello Andrew and thank you for the response and support.

I want to be able to control the elements of my chart legend. the template I presented allowed me to manipulate the legend on a more granular level .
And perhaps my question was also muddled to some extent due to my ignorance around how to best address this design decision.

I agree it is not a good practice to access Xaml DataTemplates in a ViewModel in the MVVM pattern. I did see an example here where a series template was accessed from the viewmodel .

See the example

At any rate since I am doing this as cleanly as possible using the MVVM pattern what would be the way I could solve this in the view, considering the datatemplate I am using and the

Here is an extract of my surface xaml :

<s:SciChartSurface x:Name="sciChartSurface"
                                   Grid.RowSpan="1"
                                   Grid.Column="0"
                                   Margin="35 0 0 0"
                                   s:ThemeManager.Theme="{Binding GraphTheme,
                                                                  Mode=TwoWay}"
                                   Annotations="{s:AnnotationsBinding Annotations,
                                                                      UpdateSourceTrigger=PropertyChanged}"
                                   BorderThickness="0"
                                   **ChartModifier="{Binding ChartModifier}"**

Can I bind it directly to the ChartModifier here ?
Appreciate you support
Thanks
Anders

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies