Command binding is not propagating for control int

2019-09-05 20:36发布

问题:

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>

回答1:

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.