0

For Command "CommandZoomIn", CanExecute and Execute do not occurs for controls defined into the ListBox ItemTemplate. GraphLcView method "CanExecute" and "Execute" are both called when GraphLcView UserControl is defined directly as child of AnalysisView, but both methods are never called when they are added as Item DataTemplate of a ListBox ItemTemplate.

  • The button with the command is defined in my top level window into a Ribbon.
  • Simplified hierarchy:
    • (Working) Top Level window -> AnalysisView -> GraphLcView
    • (Not working) Top Level window -> AnalysisView -> ListBox + ItemTemplate -> GraphLcView
  • The CommandBinding is defined into GraphLcView child control (UserControl.CommandBinding)
  • There is no MVVM implied into the CommandBindind

Update: I made a working sample to demo the problem but I had a different behavior than explained here. But the fully working sample should probably show something similar to what I have here. Because the bahvior is different, I asked another question at StackOverflow. Code is available here at GitHub

User Control 'GraphLcView' partial code:

<UserControl x:Class="GraphCtrl.GraphView.GraphLcView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

...

<Grid.CommandBindings>
    <CommandBinding Command="{x:Static graphCtrlCommand:CtrlAnalysisCommand.CommandZoomToFitsAll}" CanExecute="CanZoomToFitsAll" Executed="ZoomToFitsAll"/>
    <CommandBinding Command="{x:Static graphCtrlCommand:CtrlAnalysisCommand.CommandZoomIn}" CanExecute="CanZoomIn" Executed="ZoomIn"/>
    <CommandBinding Command="{x:Static graphCtrlCommand:CtrlAnalysisCommand.CommandZoomOut}" CanExecute="CanZoomOut" Executed="ZoomOut"/>

UserControl AnalysisView partial code (where previous GraphLcView UserControl is used):

                <!-- ********************************-->
                <!-- ********************************-->
                <!-- CommmandBinding works fine here -->
                <!-- ********************************-->
                <!-- ********************************-->
                <graphView1:GraphLcView Grid.Row="1" x:Name="GraphView" Graph="{Binding Graph}" 
                                        Visibility="{Binding IsMain, Converter={StaticResource BooleanToVisibilityConverter1}}"
                                        TrackedSignal="{Binding DataContext.LastTrackedSignal, Mode=TwoWay, ElementName=MyControl}"
                                        SourceTrackedSignal ="{Binding Model.EventTrackingSourceGraphToLegend, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ElementName=MyControl}"
                                        IsMouseInteractive="{Binding IsMouseInteractive}"
                                        UseFastTooltip="{Binding UseFastTooltip}"
                                        ActiveObjectChanged="OnChildActiveObjectChanged"
                                        >
                </graphView1:GraphLcView>

                <Grid Name="GridDetails" Grid.Row="1" >
                    <ListBox Name="ListBoxDetails"  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                         ItemsSource="{Binding Graph.AdditionalViews}"
                         Visibility="{Binding IsDetails, Converter={StaticResource BooleanToVisibilityConverter1}}"
                         HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel IsItemsHost="True"
                                       Name="DetailsWrapPanel"/>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>

                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Border BorderBrush="Black" BorderThickness="1" Margin="0,1,0,1"
                                        Width="{Binding DataContext.DetailsWorkspaceDimensionX, ElementName=MyControl, Mode=OneWay}"
                                        Height="{Binding DataContext.DetailsWorkspaceDimensionY, ElementName=MyControl, Mode=OneWay}"
                                        >
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"></RowDefinition>
                                            <RowDefinition></RowDefinition>
                                            <RowDefinition Height="Auto"></RowDefinition>
                                        </Grid.RowDefinitions>

                                        <TextBlock Grid.Row="0" Text="{Binding Name}"></TextBlock>

                                        <!-- ********************************-->
                                        <!-- ********************************-->
                                        <!-- Binding does not work fine here -->
                                        <!-- ********************************-->
                                        <!-- ********************************-->

                                        <!--ActiveObjectChanged="GraphLcViewDetailOnActiveObjectChanged"-->
                                        <!--SourceTrackedSignal="{Binding DataContext.EventTypeSourceForSignalTrackingToGraph, Mode=TwoWay, ElementName=MyControl}"-->
                                        <graphView1:GraphLcView Grid.Row="1"
                                            AdditionalView="{Binding Path=., Mode=OneWay}"
                                            Graph="{Binding Graph, ElementName=GraphView}" 
                                            TrackedSignal="{Binding DataContext.LastTrackedSignal, Mode=TwoWay, ElementName=MyControl}"
                                            SourceTrackedSignal ="{Binding Model.EventTrackingSourceGraphToLegend, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ElementName=MyControl}"
                                            IsMouseInteractive="{Binding IsMouseInteractive}"
                                            UseFastTooltip="{Binding UseFastTooltip}"
                                            ActiveObjectChanged="OnChildActiveObjectChanged"
                                            >
                                        </graphView1:GraphLcView>
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
Community
  • 1
  • 1
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • Can you explain more about the graphview and command. If possible add more code.. – Ayyappan Subramanian Oct 03 '16 at 15:38
  • Ok, It should take few hours but I will try to make a full working simplified sample. – Eric Ouellet Oct 03 '16 at 15:45
  • Full sample will help.. – Ayyappan Subramanian Oct 03 '16 at 16:07
  • Where those `Grid.CommandBindings` reside ? – AnjumSKhan Oct 03 '16 at 16:23
  • @AnjumSKhan, into GraphLcView. Grid is on the top of the GraphLcView. – Eric Ouellet Oct 03 '16 at 17:29
  • 1
    @EricOuellet Shift those CommandBindings into Window and tell what happens. – AnjumSKhan Oct 03 '16 at 18:24
  • @AnjumSKhan, it will work. But the problem with that way is that you break the first rule or good coding: Hi cohesion, Low coupling. I do UserControl to keep everything that is self related at the same place. If I do place the command binding elsewhere than the UserControl, my control will not be self containing. There should be a more elegant way to acheive the work. I also found something else which seems to be bad also: CommandBindings.AddRange(userControl.CommandBindings); – Eric Ouellet Oct 03 '16 at 20:26
  • 1
    @EricOuellet Also see if we shift those CommandBindings into `UserControl.CommandBindings` – AnjumSKhan Oct 04 '16 at 03:40
  • @AnjumSKhan, Thanks a lot for your help. The problem was related to a weird behavior into the LightningChart control. See my answer for more information about it. Sorry for the delay, your feedback + few more reading sent me to the proper way. Thanks. – Eric Ouellet Oct 07 '16 at 19:46

1 Answers1

0

I'm sorry. After investigation, I realised that the problem came from the LightningChart control which does not keep the focus. I added 2 handlers for "GotFocus" and "LostFocus". Then I realised that everything went fine for the control in the first tab, which is not part of a ListBox itemTemplate. But all others, that are in the second tab, into a ListBox itemTemplate, lost the focus as soon as they got it for no particular reason (at least none that I can understand).

I sent the problem to Arction, the company under LightningChart and they told me they will try to fix it soon.

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119