XAML Itemscontrol binding to property outside of the control

折月煮酒 提交于 2020-01-30 11:24:51

问题


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>

回答1:


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}}"/>



回答2:


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



来源:https://stackoverflow.com/questions/28813718/xaml-itemscontrol-binding-to-property-outside-of-the-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!