获取绑定属性类中的其他属性

发布于 2024-12-07 03:34:14 字数 648 浏览 0 评论 0原文

如果我有一个要绑定的嵌套类,我如何检索该父类。例如,我已绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那片花海 2024-12-14 03:34:14

您只能绑定到 SecondClass.ImageSource,因为 ImageDataContextFirstClass 的实例。所以在处理程序中你只需要强制转换它:

private void Image_MouseUp_1(object sender, MouseButtonEventArgs e) {
     FirstClass item = (sender as Image).DataContext as FirstClass;
     //<Change item.SecondClass or do whatever you want>
}

You can only bind to SecondClass.ImageSource because the DataContext of the Image is an instance of FirstClass. So in the handler you only need to cast it:

private void Image_MouseUp_1(object sender, MouseButtonEventArgs e) {
     FirstClass item = (sender as Image).DataContext as FirstClass;
     //<Change item.SecondClass or do whatever you want>
}
花伊自在美 2024-12-14 03:34:14

您的数据对象需要知道它的父对象,或者您可以使用 RelativeSourceElementName 绑定来引用父对象。例如

<Window x:Name="RootWindow" DataContext="{Binding FirstClass}">
    <Grid DataContext="{Binding SecondClass.SomeProperty}">

        <!-- Binding using ElementName -->
        <Button Context="{Binding ElementName=RootWindow, 
                Path=DataContext.SecondClass}" />

        <!-- Binding using RelativeSource -->
        <Button Context="{Binding RelativeSource=
                {RelativeSource AncestorType={x:Type Window}}, 
                Path=DataContext.SecondClass}" />
    </Grid>
</Window>

Your Data Object either needs to know it's parent object, or you can use a RelativeSource or ElementName binding to reference a parent object. For example

<Window x:Name="RootWindow" DataContext="{Binding FirstClass}">
    <Grid DataContext="{Binding SecondClass.SomeProperty}">

        <!-- Binding using ElementName -->
        <Button Context="{Binding ElementName=RootWindow, 
                Path=DataContext.SecondClass}" />

        <!-- Binding using RelativeSource -->
        <Button Context="{Binding RelativeSource=
                {RelativeSource AncestorType={x:Type Window}}, 
                Path=DataContext.SecondClass}" />
    </Grid>
</Window>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文