【发布时间】:2014-07-12 23:07:40
【问题描述】:
我想获取点击的 UIElement 的子元素,即按钮。也许有一个简单而简短的解决方案?我已经搜索了一段时间,但找不到易于理解和使用的解决方案。我将不胜感激与此问题相关的任何帮助。
我现在拥有的代码:
private new void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender == (sender as UIElement))
{
//TODO: getting controls name that is placed inside clicked UIElement
}
}
编辑:
想提一下 UIElement 是使用 ResourceDictionary 模板的 ContentControl。
我的 xaml 代码看起来像这样
<ContentControl Style="{StaticResource DesignerItemStyle}">
<Button x:Name="btnAdd" Content="add function" IsHitTestVisible="True"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</ContentControl>
【问题讨论】:
-
这取决于你在这里指的是什么 UIElement,例如如果那个 UIElement 是事件的所有者
MouseLeftButtonDown与处理程序相关联,你可以使用sender或e.Source,否则您可能需要查看e.OriginalSource。