在UserControl中捕获鼠标单击

时间:2017-05-23 08:02:07

标签: c# wpf mouseevent

我创建了一个包含多个标签的UserControl。另一方面,我有一个窗口(简单网格),它创建了6个UserControl实例并将它们放在一行中。

我的问题是:当用户点击userControl(其任何部分)时,如何启动某些操作?

我尝试在UserControl MouseDown文件中添加cs事件处理程序,并在创建UserControl实例期间在父窗口内添加,但这没有任何效果。我还尝试添加PreviewMouseLeftButtonDownMouseEnterMouseLeftButtonDownButton,但这些都没有效果。

这是父窗口的一部分:

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);
        }
    }

这是UserControl:

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>

1 个答案:

答案 0 :(得分: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的{​​{1}} DataContext

其中UserControl是您在该控件上UserControlClicked时调用的公共方法。

这是我的UserControlViewModel,只是为了显示目的,并进行测试。

MouseLeftButtonDown

在MainWindow内部我实例化了几个UserControl-s:

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));
    }
}

这似乎按预期工作。已点击的UserControl已更改已开始的标签内容!点击!。

编辑:检查一下如何通过Blend添加CallMethodAction: CallMethodAction