简体   繁体   中英

How to get a parent value in multibinding

I'm using dataTemplate . This is the template:

   <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>

Can you see this line?

<Binding Path="RAM.Size" />

That line throws me an exception, it should be because RAM.Size is from a parent element. How might I get that value?

Thanks in advance!

So you're trying to get to the RAM.Size value on the same object that your ItemsControl is getting its ItemsSource from?

See if this works:

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

So the binding is going up in through the visual tree to the ItemsControl, then binding to the Ram.Size property of its DataContext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM