【发布时间】:2016-07-23 19:58:54
【问题描述】:
我有一个listbox。 listbox DataTemplate 由少数Text Blocks 和一些TextBoxes 组成。
问题在于鼠标双击我需要找出双击是在哪个元素上完成的,以便做一些进一步的操作,比如使TextBox可编辑等等。同时我还需要为列表双击定义一些动作。所以我不能为每个组件单独处理鼠标。
因此,我需要为ListBox 按下鼠标并找出双击是在哪个元素上进行的。
我尝试使用以下代码,但它返回 ListBox 的 Name 而不是 TextBox 的 Name:
private void myListBox_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var mouseWasDownOn = e.Source as FrameworkElement;
if (mouseWasDownOn != null)
{
string elementName = mouseWasDownOn.Name;
}
}
我也尝试过以下问题
WPF Get Element(s) under mouse
How to know what control the mouse has clicked in a canvas?
Getting the Logical UIElement under the mouse in WPF
public void ListBox_MouseDownHandler(object sender, MouseButtonEventArgs e)
{
HitTestResult target = VisualTreeHelper.HitTest(myListBoxName, e.GetPosition(myListBoxName));
while(!(target is Control) && (target != null))
{
target = VisualTreeHelper.GetParent(target);
}
}
但仍然找不到解决方案。因此,请帮助我通过双击获取元素类型或元素名称。
【问题讨论】:
-
是否可以使用ItemTemplate?
-
哦,你是想说,如果我在后面的代码中处理数据模板的双击事件,我可以获得元素类型或名称吗?
-
你能展示你的 XAML 代码吗?
-
正如@S.Akbari 所说,您需要向我们展示您的 XAML 代码。但在我的脑海中,你可以使用 Style.Triggers->EventTrigger->BeginStoryboard-> Animate IsReadOnly 在离散步骤中设置为 false,如果这就是你所需要的