命令绑定不向控件传播到ItemTemplate的DataTemplate中 [英] Command binding is not propagating for control into a DataTemplate of ItemTemplate

查看:84
本文介绍了命令绑定不向控件传播到ItemTemplate的DataTemplate中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于命令 CommandZoomIn,对于在ListBox ItemTemplate中定义的控件,不会执行CanExecute和Execute。直接将GraphLcView UserControl定义为AnalysisView的子级时,将同时调用GraphLcView方法 CanExecute和 Execute,但是在将它们作为ListBox ItemTemplate的Item DataTemplate添加时,永远不会调用这两种方法。

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.


  • 带有命令的按钮在我的顶级窗口中定义为功能区。

  • 简化的层次结构:


    • (工作中)顶层窗口-> AnalysisView-> GraphLcView

    • (不起作用)顶层窗口-> AnalysisView-> ListBox + ItemTemplate-> GraphLcView

    更新:我制作了一个工作示例来演示该问题,但是行为与此处说明的有所不同。但是,可以正常工作的示例可能应该显示与我在此处显示的内容相似的内容。由于行为方式不同,我问了 StackOverflow上的另一个问题代码在GitHub上可用

    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控制'GraphLcView'部分代码:

    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部分代码(使用以前的GraphLcView UserControl):

    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>
    


    推荐答案

    对不起。经过调查,我意识到问题出在LightningChart控件上,该控件无法保持焦点。我为 GotFocus和 LostFocus添加了两个处理程序。然后,我意识到第一个选项卡中的控件一切正常,它不是ListBox itemTemplate的一部分。但是,所有其他对象(位于第二个选项卡中,进入ListBox itemTemplate)在没有特定原因(至少我无法理解)的情况下就失去了焦点。

    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).

    我将问题发送给了LightningChart旗下的Arction公司,他们告诉我他们将尽快解决。

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

    这篇关于命令绑定不向控件传播到ItemTemplate的DataTemplate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆