WPF Databinding: ¿Cómo puedo acceder al contexto de datos "padre"?
Tengo una lista (ver más abajo) contenida en una ventana. La ventana DataContext
tiene dos propiedades, Items
y AllowItemCommand
.
¿Cómo obtengo el enlace para la propiedad Hyperlink
's Command
que necesita resolver contra la DataContext
de la ventana?
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="Action">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock>
<!-- this binding is not working -->
<Hyperlink Command="{Binding AllowItemCommand}"
CommandParameter="{Binding .}">
<TextBlock Text="Allow" />
</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
3 answers
Podrías intentar algo como esto:
...Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-14 20:51:38
Esto también funcionará:
<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
Path=DataContext.AllowItemCommand}" />
ListView
heredará su DataContext
de Window
, por lo que también está disponible en este punto.
Y desde ListView
, al igual que controles similares (e. g. Gridview
, ListBox
, etc.), es una subclase de ItemsControl
, el Binding
para tales controles funcionará perfectamente.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-01 12:06:33
Esto también funciona en Silverlight 5 (quizás también antes, pero no lo he probado). Usé la fuente relativa así y funcionó bien.
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-11 06:43:28