我正在做一个WinUI3应用程序,我做了一个用户控件,我一直在尝试让左点击和右点击来做一些事情。由于某种原因,我的组件不能像其他组件那样接收事件。
下面是我的XAML代码:
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="esheets_desktop_app_trafalgareng.Task"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:esheets_desktop_app_trafalgareng"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Name="stackPanel" Orientation="Horizontal" PointerPressed="Task_PointerPressed" PointerEntered="stackPanel_PointerEntered">
<TextBlock Name="projectNum" Width="100" Margin="10,10,10,10" TextAlignment="Center"/>
<TextBlock Name="taskName" Width="750" Margin="10,10,10,10" TextAlignment="Left"/>
<TextBlock Name="assignedBy" Width="100" Margin="10,10,10,10" TextAlignment="Left"/>
</StackPanel>
</UserControl>
我的CS代码:
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System;
using System.Diagnostics;
namespace esheets_desktop_app_trafalgareng
{
public sealed partial class Task : UserControl
{
internal int TaskID { get; set; }
internal int? ProjectID { get; set; }
internal int TaskTypeID { get; set; }
internal int? TaskAssignee { get; set; }
internal string TaskName { get; set; }
internal DateTime? TaskDueDate { get; set; }
internal int TaskStatus { get; set; }
internal string TaskPriority { get; set; }
internal int TaskCreatedBy { get; set; }
internal DateTime? TaskDateCreated { get; set; }
internal int TaskLastEditedBy { get; set; }
internal DateTime? TaskDateEdited { get; set; }
internal bool TaskSent { get; set; }
internal DateTime? TaskDateSent { get; set; }
public Task(int TaskID, int? ProjectID, int TaskTypeID, int? TaskAssignee, string TaskName, DateTime? TaskDueDate, int TaskStatus, string TaskPriority, int TaskCreatedBy, DateTime? TaskDateCreated, int TaskLastEditedBy, DateTime? TaskDateEdited, bool TaskSent, DateTime? TaskDateSent)
{
this.TaskID = TaskID;
this.ProjectID = ProjectID;
this.TaskTypeID = TaskTypeID;
this.TaskAssignee = TaskAssignee;
this.TaskName = TaskName;
this.TaskDueDate = TaskDueDate;
this.TaskStatus = TaskStatus;
this.TaskPriority = TaskPriority;
this.TaskCreatedBy = TaskCreatedBy;
this.TaskDateCreated = TaskDateCreated;
this.TaskLastEditedBy = TaskLastEditedBy;
this.TaskDateEdited = TaskDateEdited;
this.TaskSent = TaskSent;
this.TaskDateSent = TaskDateSent;
this.InitializeComponent();
projectNum.Text = Tasks.GetProjectNumber(ProjectID);
taskName.Text = this.TaskName;
assignedBy.Text = Tasks.GetName(TaskCreatedBy);
if (TaskDueDate != null && ((DateTime)TaskDueDate).Date == DateTime.Today)
{
SolidColorBrush redBrush = new(Windows.UI.Color.FromArgb(255, 255, 0, 0));
projectNum.Foreground = redBrush;
taskName.Foreground = redBrush;
assignedBy.Foreground = redBrush;
}
if (TaskStatus == TaskStatuses.Completed)
{
SolidColorBrush grayBrush = new(Windows.UI.Color.FromArgb(255, 64, 64, 64));
projectNum.Foreground = grayBrush;
taskName.Foreground = grayBrush;
assignedBy.Foreground = grayBrush;
}
}
private void Task_PointerPressed(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
{
if (TaskStatus == TaskStatuses.Completed) TaskStatus = TaskStatuses.Not_Completed;
else TaskStatus = TaskStatuses.Completed;
Tasks.Page.RedrawActive();
}
private void stackPanel_PointerEntered(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Trace.WriteLine("test");
}
}
我试过把PointerPressed
和PointerEntered
事件放在UserControl
、StackPanel
和TextBlock
上,但到目前为止还没有任何效果。
1条答案
按热度按时间ffvjumwh1#
就我而言,你应该尝试设置UserControl的
Background property
。你可以参考thread:Pointer events not triggered on UI Elements in custom control