获得元素的名称上,我`clicked` /`mousedown` [英] get the name of the element on which I `clicked` / `mousedown`

查看:133
本文介绍了获得元素的名称上,我`clicked` /`mousedown`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的根元素是一个名为图标的页面。



我有一个像<许多控制code>的TextBlock ,文本框电网矩形边框等...谁是

的孩子

现在,我想有一个的MouseDown PreviewMouseDown 点击来找到我点击的元素的名称。



所以,我怎样才能得到我点击的元素的名称 / 鼠标按下



如果我能得到一个使用隧道的解决方案,这将是容易得多。


解决方案

RoutedEventArgs 有获取或设置对引发事件的对象的引用属性源。试试这个




xaml.cs




 私人无效Grid_MouseDown_1(对象发件人,MouseButtonEventArgs E)
{
VAR mouseWasDownOn = e.Source为FrameworkElement的;
如果(mouseWasDownOn!= NULL)
{
串的ElementName = mouseWasDownOn.Name;
}
}




XAML




 <电网PreviewMouseDown =Grid_MouseDown_1> 
<&StackPanel的GT;
<组合框的ItemsSource ={绑定的ItemsSource}/>
<按钮内容=OK/>
< / StackPanel的>

< /网格和GT;



Khushi萨拉项目计算器walon他本身卡拉那海KYA:)


I have a page whose root element is a Grid named Root.

I have many controls like TextBlock, TextBox, Grid, Rectangle, Border etc... who are children of Root.

Now, I want to have a MouseDown or PreviewMouseDown or Click on Root to find the name of the element on which I clicked.

So, how can I get the name of the element on which I clicked / mousedown ?

If I can get a solution that uses Tunneling, It will be much easier.

解决方案

RoutedEventArgs has property Source that Gets or sets a reference to the object that raised the event. Try this

xaml.cs

private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
    {
        var mouseWasDownOn = e.Source as FrameworkElement;
        if (mouseWasDownOn != null)
        {
            string elementName = mouseWasDownOn.Name;
        }
    }

xaml

<Grid PreviewMouseDown="Grid_MouseDown_1">
    <StackPanel>
        <ComboBox ItemsSource="{Binding ItemsSource}"/>
        <Button Content="ok"/>
    </StackPanel>

</Grid>

Khushi sara project stackoverflow walon se he karana hai kya :)

这篇关于获得元素的名称上,我`clicked` /`mousedown`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆