获取绑定属性类中的其他属性
如果我有一个要绑定的嵌套类,我如何检索该父类。例如,我已绑定到 SecondClass.ImageSource。我现在想在点击图像时获取SecondClass的信息,我该如何实现呢?
我想更改 SecondClass 属性,但问题是我有一个包含相同图像项目的网格。
图像的绑定已经起作用。
XAML:
<Image Source="{Binding Path=SecondClass.ImageSource}" Width="48" Height="48" MouseUp="Image_MouseUp_1" />
代码:
public class FirstClass {
public int number { get; set; }
public SecondClass SecondClass
}
public class SecondClass {
public ImageSource ImageSource { get; set; }
}
private void Image_MouseUp_1(object sender, MouseButtonEventArgs e) {
FirstClass item = ????
}
If I got a nested class that I am binding to, how could I retrieve that parent class. For example, I have bound to SecondClass.ImageSource. I now want to get the information of the SecondClass when I click on the Image, how can I achieve this?
I would like to change the SecondClass property, but the problem is that I have a grid of the same image items.
The binding of the image works already.
Xaml:
<Image Source="{Binding Path=SecondClass.ImageSource}" Width="48" Height="48" MouseUp="Image_MouseUp_1" />
Code:
public class FirstClass {
public int number { get; set; }
public SecondClass SecondClass
}
public class SecondClass {
public ImageSource ImageSource { get; set; }
}
private void Image_MouseUp_1(object sender, MouseButtonEventArgs e) {
FirstClass item = ????
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能绑定到
SecondClass.ImageSource
,因为Image
的DataContext
是FirstClass
的实例。所以在处理程序中你只需要强制转换它:You can only bind to
SecondClass.ImageSource
because theDataContext
of theImage
is an instance ofFirstClass
. So in the handler you only need to cast it:您的数据对象需要知道它的父对象,或者您可以使用
RelativeSource
或ElementName
绑定来引用父对象。例如Your Data Object either needs to know it's parent object, or you can use a
RelativeSource
orElementName
binding to reference a parent object. For example