c# - 如何在多重绑定(bind)中获取父值

标签 c# wpf data-binding binding datatemplate

我正在使用 dataTemplate。这是模板:

   <ItemsControl ItemsSource="{Binding RAM.Partitions}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding Position, StringFormat={}{0}k}"/>
                    <Grid Grid.Column="1">
                        <Border>
                            <Border.Height>
                                <MultiBinding Converter="{StaticResource MultiplyConverter}">
                                    <Binding ElementName="LayoutRoot" Path="ActualHeight"/>
                                    <Binding Path="Size" />
                                    <Binding Path="RAM.Size" />
                                </MultiBinding>
                            </Border.Height>
                        </Border>
                    </Grid>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

你能看到这条线吗?

<Binding Path="RAM.Size" />

那一行抛出异常,应该是因为 RAM.Size 来自父元素。我如何获得该值(value)?

提前致谢!

最佳答案

因此,您正试图获取 ItemsControl 从中获取其 ItemsSource 的同一对象的 RAM.Size 值?

看看这是否有效:

<MultiBinding Converter="{StaticResource MultiplyConverter}"> 
    <Binding ElementName="LayoutRoot" Path="ActualHeight"/> 
    <Binding Path="Size" /> 
    <Binding Path="DataContext.RAM.Size"
        RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}" /> 
</MultiBinding>

因此绑定(bind)通过可视化树上升到 ItemsControl,然后绑定(bind)到其 DataContext 的 Ram.Size 属性。

关于c# - 如何在多重绑定(bind)中获取父值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9714134/

相关文章:

c# - 使用 Zxing.Net Mobile 的表格无法识别

c# - 使用 ajax 将模型发送到 Controller ,asp.net mvc

具有多列的 WPF ItemsControl

c# - 双向绑定(bind)到 WPF 中多个列表框上的 ListBox SelectedItem

c# - TreeView 不显示更新

c# - 显示月份名称的 DataAnnotation

c# - DataGrid.Items.Count 不按预期工作

c# - 使用 CI 服务器自动部署

c# - DataGrid header 中的 WPF 数据绑定(bind)

wpf - 更新 DataTemplate 内 DataTemplate 的绑定(bind)