2016-11-26 57 views
0

我注意到一個奇怪的bug。當我將一個命令綁定到佈局MouseLeftButtonDown事件時,它不會觸發。我試圖調試並注意到它會觸發,但僅在初始化期間。我想關鍵在於約束力。下面的代碼:-wpf帆布事件只發射第一次

<ItemsControl Grid.Row="1" ItemsSource="{Binding Polygons}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas> 
        <i:Interaction.Behaviors> 
         <behaviours:MouseBehaviour MouseX="{Binding MouseX, Mode=OneWayToSource}" MouseY="{Binding MouseY, Mode=OneWayToSource}" /> 
        </i:Interaction.Behaviors> 

        <i:Interaction.Triggers> 
         <i:EventTrigger EventName="MouseLeftButtonDown"> 
          <command:EventToCommand Command="{Binding SelectPointCommand}"/> 
         </i:EventTrigger> 
        </i:Interaction.Triggers> 
       </Canvas> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       /* some data template 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

和命令執行:

public ICommand SelectPointCommand 
    { 
     get 
     { 
      if (!CanEdit) 
       return new RelayCommand(e => { }); 

      ClickCounter++; 

      if (ClickCounter == 3) 
      { 
       ClickCounter = 0; 
       CanEdit = false; 
      } 

      return new RelayCommand(
       () => 
       { 
        Polygons.Add(new Polygon(ClickedPoints)); 
        ClickedPoints.Clear(); 
       }); 
     } 
    } 

我猜這裏的問題在MouseBehaviour但刪除這段代碼也沒有幫助。

ps:我試着設置畫布背景屬性,它沒有工作。 以及設置命令這

SelectPointCommand = new RelayCommand(
     () => 
     { 
      System.Windows.MessageBox.Show("Test"); 
     }, 
     () => true); 

編輯 席力圖召方法是這樣的:

<Canvas Background="Transparent" MouseLeftButtonDown="UIElement_OnMouseLeftButtonDown"> 

</Canvas> 

而後面的代碼:

private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     ((MainViewModel)DataContext).SelectPointCommand.Execute(e); 
    } 

方法UIElement_OnMouseLeftButtonDown ISN」無論如何, 將Canvas更改爲StackPanel具有相同的結果。

回答

0

好好,我固定這個錯誤。問題出在這裏:

<Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto"/> 
</Grid.RowDefinitions> 

將行的高度設置爲「自動」意味着將其設置爲零。所以畫布不存在!我離開它是這樣的:

<RowDefinition/> 

之後,一切工作得很好。

0

由於您沒有發佈所有代碼,因此很難檢查出現問題。 CanEdit屬性在其他地方更改?什麼是ClickCounter?

我認爲這個問題是SelectPointCommand的吸氣劑。它僅在創建綁定時執行一次。我也會利用ICommand的CanExecute方法並將getter的返回值存儲在私有字段中。例如:

private ICommand _selectPointCommand; 
    ICommand SelectPointCommand 
    { 
     get 
     { 
      Console.WriteLine("This is executed once"); 
      return _selectPointCommand; 
     } 

     set 
     { 
      if (_selectPointCommand != value) 
      { 
       _selectPointCommand = value; 
       OnPropertyChanged("SelectPointCommand"); 
      } 
     } 
    } 

在視圖模型的構造:

 SelectPointCommand = new RelayCommand(
       (x) => 
       { 
        Console.WriteLine("This is executed every click"); 
        ClickCounter++; 

        if (ClickCounter == 3) 
        { 
         ClickCounter = 0; 
         CanEdit = false; 
        } 
        Polygons.Add(new Polygon(ClickedPoints)); 
        ClickedPoints.Clear(); 
       }, 
       (x) => { return CanEdit; }); 
+0

我試過你的修正,但它仍然只執行一次,不幸的 –