devfunkd
devfunkd

Reputation: 3244

XAML Itemscontrol binding to property outside of the control

I have a list of transactions that I am loading into an ItemsControl but I want to show the UserId from my viewmodel in each row/transaction. The userId is not in the transaction list, it's a property in the viewmodel I am trying to access but it seems everything I've tried doesn't work and I don't know why. All I get is a blank column value. (yes, I did confirm the property is not empty. even hardcoded a value and got nothing).

My understanding is I should be able to use the RelativeSource with an AncestorType of Window to access the property. Am I missing something?

<ItemsControl ItemsSource="{Binding Path=MyReportResult.Transactions}" KeyboardNavigation.IsTabStop="False">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="20 2 0 2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width=".15*"/>
                    <ColumnDefinition Width=".15*"/>
                </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" TextAlignment="Center" Margin="10 0 20 0" Text="{Binding Path=UserId, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"></TextBlock>
            <TextBlock Grid.Column="0" TextAlignment="Center" Margin="10 0 20 0" Text="{Binding Path=Amount}"></TextBlock>
            </Grid>
         </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 0

Views: 1438

Answers (2)

Chris Bordeman
Chris Bordeman

Reputation: 235

Only WPF supports RelativeSource because it's not strictly necessary. It's less fragile to use ElementName instead like so:

First, give an x:Name="something" to your top level object or its child (usually a Grid).

Text="{Binding Path=DataContext.UserId, Element=something}"

Upvotes: 0

Clemens
Clemens

Reputation: 128146

Your RelativeSource binding tries to find the property in the parent Window, although it is in the Window's DataContext.

The following should work, provided that the UserId property is in the same view model class as MyReportResult:

<TextBlock Text="{Binding Path=DataContext.UserId,
                  RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>

Upvotes: 1

Related Questions