WPF从代码后面获取关联标签

时间:2014-06-30 09:50:31

标签: c# wpf textbox label

我想从后面的代码中获取关联的标签:

例如:

<Label Name="fooLabel" Target={x:Reference fooTextBox} />
<TextBox Name="fooTextBox" />

伪代码(代码背后):

if (object.Equals(fooTextBox.AssociatedLabel, fooLabel) == true)
{
    //yeah...
}

(我知道我可以反过来做,fooLabel.Target会给我TextBox但我没有标签,我think there should be a better way比搜索Visual Tree 1}}用于所有标签并检查其respective Target

1 个答案:

答案 0 :(得分:0)

您可以使用FindName方法

Label lbl = (Label)this.FindName("fooLabel");