How to get a parent value in multibinding
Asked Answered
K

1

12

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!

Kulturkampf answered 15/3, 2012 at 4:47 Comment(0)
L
40

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.

Longcloth answered 15/3, 2012 at 4:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.