对于命令“CommandZoomIn”,对于定义到ListBox ItemTemplate中的控件,CanExecute和Execute不会发生。 GraphLcView方法“CanExecute”和“Execute”都是在GraphLcView UserControl被直接定义为AnalysisView的子类时调用的,但是这两种方法在被添加为ListBox ItemTemplate的Item DataTemplate时都不会被调用。命令绑定不会传播控制到ItemTemplate的DataTemplate中
- 带有该命令的按钮在我的顶级窗口中定义为功能区。
- 简化层次:
- (工作)的顶层窗口 - > AnalysisView - > GraphLcView
- (不工作)的顶层窗口 - > AnalysisView - >列表框+的ItemTemplate - > GraphLcView
- CommandBinding被定义为GraphLcView子控件(UserControl.CommandBinding)
- CommandBindind中没有暗示MVVM
更新:我做了一个工作示例来演示问题,但我的行为与此处解释的不同。但是完整的工作样本应该可能会显示类似于我在这里的内容。因为行为不同,我问another question at StackOverflow。 Code is available here at GitHub
用户控制 'GraphLcView' 部分编码:
<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"/>
用户控件AnalysisView部分码(其中使用前GraphLcView用户控件):
<!-- ********************************-->
<!-- ********************************-->
<!-- 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>
你能解释更多关于graphview和命令吗?如果可能的话添加更多的代码.. –
好的,它应该需要几个小时,但我会尽量做一个完整的工作简化样本。 –
Full sample will help .. –