Can I change the binding source to another source in XAML?
I want to change the source in XAML to another object source. For example:
- I have a Listview, part of a window bound to a "Model A", it has many properties, but one is called "Total". This property is not shown on the View
- Each ListviewItem has its own source (ItemsSource), BUT, one of the fields should show "Total" from "Model A"
Caveat: I am implementing MVVM, so the "Model A" is assigned to the Window 开发者_运维技巧as a VM DataSource, XAML knows nothing about it.
View.DataSource = MyViewModel (that contains "Model A")
So, in my perfect world the XAML should look like this:
<GridViewColumn Header="Total" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Source=<The source of WINDOW> Path=Total}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I have tried RelativeSource but that points me to the XAML object, I want the source of it, is it possible? Can someone point me in the right direction?
<Label Content="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=Total}"/>
WPF Binding Cheat Sheet
精彩评论