来自DataTemplate的InvokeCommandAction未触发?

时间:2018-03-08 14:10:13

标签: c# .net wpf xaml

我试图在DataTemplate中使用InvokeCommandAction将视图事件挂接到DataContext视图模型中的ICommand,但是从不调用命令execute方法。如果我提出事件并处理后面的视图代码,那么我得到预期的行为......

家长视图

  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

 <DataTemplate x:Shared="False" DataType="{x:Type sensors:CleanSensorsViewModel}">
      <local:CleanTimeSeriesView>
            <i:Interaction.Triggers>
              <i:EventTrigger EventName="IsVisibleChanged">
                 <i:InvokeCommandAction Command="{Binding SensorTabVisibilityChangedCommand, diag:PresentationTraceSources.TraceLevel=High}"/>
              </i:EventTrigger>
          </i:Interaction.Triggers>
     </local:CleanTimeSeriesView>
  </DataTemplate>

使用DataTemplate在视图中进行控制

<docking:DockSite Grid.Column="2" 
                              ToolItemsSource="{Binding MeasuredSignalViewModels}"
                              NewWindowRequested="OnNewTabRequested"
                              WindowsClosed="OnWindowsClosed"
                              AreNewTabsInsertedBeforeExistingTabs="False"
                              ToolItemContainerStyleSelector="{StaticResource StyleSelector}">
                <docking:Workspace>
                    <docking:TabbedMdiHost ContainersHaveNewTabButtons="True" TabOverflowBehavior="ScrollWithMenu" />
                </docking:Workspace>
            </docking:DockSite>

DataTemplate CleanSensorsViewModel是MeasuredSignalViewModels中的项目

数据模板的视图模型(CleanSensorsViewModel)

 public ICommand SensorTabVisibilityChangedCommand { get; set; }

 private void InitialiseCommands()
 {           
   this.SensorTabVisibilityChangedCommand = new DelegateCommand(this.ExecuteSensorTabVisibleChangedCommand);
 }

 private void ExecuteSensorTabVisibleChangedCommand()
 {
   //Breakpoint here
 }

我已开启跟踪(参见xaml)&amp;这就是我在输出窗口中得到的内容

System.Windows.Data Warning: 56 : Created BindingExpression (hash=27231335) 
for Binding (hash=58769117)
System.Windows.Data Warning: 58 :   Path: 
'SensorTabVisibilityChangedCommand'
System.Windows.Data Warning: 60 : BindingExpression (hash=27231335): Default 
mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=27231335): Default 
update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=27231335): Attach 
to System.Windows.Interactivity.InvokeCommandAction.Command (hash=66182225)
System.Windows.Data Warning: 64 : BindingExpression (hash=27231335): Use 
Framework mentor <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=27231335): 
 Resolving source 
System.Windows.Data Warning: 69 : BindingExpression 
(hash=27231335):Framework 
mentor not found
System.Windows.Data Warning: 65 : BindingExpression (hash=27231335): Resolve 
source deferred
System.Windows.Data Warning: 95 : BindingExpression (hash=27231335): Got 
InheritanceContextChanged event from InvokeCommandAction (hash=66182225)
System.Windows.Data Warning: 67 : BindingExpression (hash=27231335): 
Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=27231335): 
Framework mentor not found
System.Windows.Data Warning: 95 : BindingExpression (hash=27231335): Got 
InheritanceContextChanged event from InvokeCommandAction (hash=66182225)
System.Windows.Data Warning: 67 : BindingExpression (hash=27231335): 
Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=27231335): Found 
data 
context element: CleanTimeSeriesView (hash=46207013) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=27231335): 
Activate with root item CleanSensorsViewModel (hash=15858029)
System.Windows.Data Warning: 108 : BindingExpression (hash=27231335):   At 
level 0 - for CleanSensorsViewModel.SensorTabVisibilityChangedCommand found 
accessor RuntimePropertyInfo(SensorTabVisibilityChangedCommand)
System.Windows.Data Warning: 104 : BindingExpression (hash=27231335): 
Replace 
item at level 0 with CleanSensorsViewModel (hash=15858029), using accessor 
RuntimePropertyInfo(SensorTabVisibilityChangedCommand)
System.Windows.Data Warning: 101 : BindingExpression (hash=27231335): 
GetValue 
at level 0 from CleanSensorsViewModel (hash=15858029) using 
RuntimePropertyInfo(SensorTabVisibilityChangedCommand): DelegateCommand 
(hash=20998208)
System.Windows.Data Warning: 80 : BindingExpression (hash=27231335): 
TransferValue - got raw value DelegateCommand (hash=20998208)
System.Windows.Data Warning: 89 : BindingExpression (hash=27231335): 
TransferValue - using final value DelegateCommand (hash=20998208)

我可以告诉我这个绑定是否正确解决了?我根本没有得到任何绑定错误,但是从不调用视图模型上的execute中的断点。我有什么东西在这里失踪吗?在解决绑定时,数据模板中的控件是否存在潜在的计时问题?

0 个答案:

没有答案