2017-05-23 100 views
-1

我創建了一個擁有多個標籤的UserControl。另一方面,我有一個窗口(簡單網格),它創建6個UserControl實例並將它們放在一行中。在UserControl中捕獲鼠標點擊

我的問題是:如何在用戶點擊userControl(它的任何部分)時觸發一些動作?

我試着在UserControl cs文件中添加MouseDown事件處理程序,並在UserControl實例創建期間在父窗口內部添加事件處理程序,但這不起任何作用。我也試過添加PreviewMouseLeftButtonDownMouseEnter,MouseLeftButtonDownButton,但沒有任何工作。

這是父窗口的一部分:

public BuyerSellerMonitorGridWindow() 
    { 
     InitializeComponent(); 
     for (int i = 0; i < 6; i++) 
     { 
      // Add new column to grid 
      this.grdBuyer.ColumnDefinitions.Add(new ColumnDefinition()); 

      // Create and add new transaction 
      UserControl uc = new UserControl(); 
      uc.PreviewMouseLeftButtonDown += uc_MouseDown; 
      System.Windows.Controls.Grid.SetRow(uc, 0); 
      System.Windows.Controls.Grid.SetColumn(uc, i); 
      this.grdBuyer.Children.Add(uc); 
     } 
    } 

這是用戶控件:

public partial class Transaction: UserControl 
    { 
     public Transaction() 
     { 
      InitializeComponent(); 
     } 

     private void TransactionClicked() 
     { 
      Console.WriteLine("test"); 
     } 
    } 

這是xaml

<UserControl x:Class="AssetStudio.Dialogs.Transaction" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:AssetStudio.Dialogs" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 

    <StackPanel x:Name="frameTransaction"> 
      <Label x:Name="lblClientName" Content="Client A" Height="35"/> 
     <Label x:Name="lblDate" Content="2017/05/01"/> 
     <Grid Height="34"> 
      <Label x:Name="label" Content="Curr Px" HorizontalAlignment="Left" Width="150"/> 
      <Label x:Name="lblCurrentPrice" Content="Label" Margin="150,0,0,0"/> 
     </Grid> 
     <Grid Height="34"> 
      <Label x:Name="label2" Content="Trade Px" HorizontalAlignment="Left" Width="150"/> 
      <Label x:Name="lblTradePrice" Content="Label" Margin="150,0,0,0"/> 
     </Grid> 
     <Grid Height="34"> 
      <Label x:Name="label4" Content="Qty Done" HorizontalAlignment="Left" Width="150"/> 
      <Label x:Name="lblQuantityDone" Content="Label" Margin="150,0,0,0"/> 
     </Grid> 
     <Grid Height="34"> 
      <Label x:Name="label6" Content="Size" HorizontalAlignment="Left" Width="150"/> 
      <Label x:Name="lblSize" Content="Label" Margin="150,0,0,0"/> 
     </Grid> 
     <Grid Height="34"> 
      <Label x:Name="label8" Content="Stk Px" HorizontalAlignment="Left" Width="150"/> 
      <Label x:Name="lblStockPrice" Content="Label" Margin="150,0,0,0"/> 
     </Grid> 
     <Grid Height="34"> 
      <Label x:Name="label10" Content="Delta" HorizontalAlignment="Left" Width="150"/> 
      <Label x:Name="lblDelta" Content="Label" Margin="150,0,0,0"/> 
     </Grid> 
    </StackPanel> 

</UserControl> 
+0

當你使用的用戶控件MouseEnter事件,你需要確保你的用戶控件裏面的內容。我在這裏使用它,它沒有任何問題。 – Bojje

+0

而且我還建議在使用WPF編程時使用MVVM,那麼您將不需要這個後臺代碼。而是將屬性綁定到Xaml控制器。 – Bojje

+0

你可以顯示你的UserControl嗎?我無法在我身邊重現您的問題。 – 3615

回答

0

你可以做這樣的事情:

在你的用戶控件:

包括命名空間:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 

,並添加CallMethodAction

<i:Interaction.Triggers> 
    <i:EventTrigger EventName="MouseLeftButtonDown"> 
     <ei:CallMethodAction MethodName="UserControlClicked" 
          TargetObject="{Binding}"/> 
    </i:EventTrigger> 
</i:Interaction.Triggers> 

TargetObject="{Binding}"告訴你,這種方法UserControlClicked需要在你的ViewModel那就是UserControlDataContext

其中UserControlClicked是您在該控件上製作MouseLeftButtonDown時調用的公共方法。

這是我的UserControlViewModel只是爲了顯示目的,並進行測試。

public class UserControlViewModel : INotifyPropertyChanged 
{ 
    public UserControlViewModel() 
    { 
     Text = "Started!"; 
    } 
    private string _text; 

    public string Text 
    { 
     get { return _text; } 
     set 
     { 
      _text = value; 
      OnPropertyChanged(); 
     } 
    } 

    public void UserControlClicked() 
    { 
     Text = "Clicked!"; 
    } 

    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
    public virtual void OnPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

內部主窗口我有幾個實例化用戶控件這個-S的:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 

    <StackPanel> 
     <local:UserControl1 /> 
     <local:UserControl1 /> 
     <local:UserControl1 /> 
     <local:UserControl1 /> 
     <local:UserControl1 /> 
    </StackPanel> 

</Grid> 

這似乎是按預期工作。已點擊的用戶控件已更改已啓動的標籤內容!點擊!

編輯:入住這如何通過混合添加CallMethodAction: CallMethodAction

+0

嗨。 'ei'中不存在'CallMethodAction'。任何想法爲什麼? – Fejs

+0

添加引用到Microsoft.Expression.Interactions和System.Windows.Interactivity –

+0

Usualy我添加通過Blend CallMethodAction(點擊),它所有的所有引用和命名空間。 –