我注意到一個奇怪的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具有相同的結果。
我試過你的修正,但它仍然只執行一次,不幸的 –