對於命令「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 .. –