[英]Get a Bound Object from a control
I have the following xaml: 我有以下xaml:
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}"></Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
In my code I have an event that gives me access to the button. 在我的代码中,我有一个事件,让我可以访问该按钮。 How can I take the button object and get the object that it's Name is bound to?
如何获取按钮对象并获取其名称绑定的对象?
Here is psudo code that I would like to work: 这是我想要工作的psudo代码:
public void MyEvent(Object obj)
{
Button myButton = (Button) obj;
MyBoundClass myObject = GetBoundClassFromProperty(myButton.Name);
// Do something with myObject.
}
Try accessing the DataContext property. 尝试访问DataContext属性。 This will contain a reference to the current item the button is bound to.
这将包含对按钮绑定的当前项的引用。
public void MyEvent(Object obj)
{
Button myButton = (Button) obj;
MyBoundClass myObject = myButton.DataContext as MyBoundClass;
// Do something with myObject.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.