获得元素的名称上,我`clicked` /`mousedown` [英] get the name of the element on which I `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 ofRoot
.Now, I want to have a
MouseDown
orPreviewMouseDown
orClick
onRoot
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屋!