samedi 14 mars 2015

Binding to a static command from inside a DataTemplate


Vote count:

0




There are many references on SO about how to bind commands from your DataTemplate to your actual data class or container class but I couldn't find anything about static commands. And whatever I tried hadn't worked so far:



<ContentControl x:Class="MyClass" ...

<ContentControl.Resources>
<RoutedUICommand x:Key="DoCommand" Text="Command" />
</ContentControl.Resources>

<ContentControl.CommandBindings>
<CommandBinding Command="{StaticResource DoCommand}" Executed="Command_Handler" />
</ContentControl.CommandBindings>

<SomeContainer>
...
<DataTemplate ...>
...
<Button Command="{StaticResource DoCommand}" CommandParameter="{Binding}">
<Button Command="{Binding DoCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyClass}}}" CommandParameter="{Binding}">
<Button Command="{Binding DataContext.DoCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyClass}}}" CommandParameter="{Binding}">
...
</DataTemplate>
<SomeContainer>

<Button Name="ThisWorks" Command="{StaticResource DoCommand}" CommandParameter="{Binding}">

</ContentControl>


None of those template buttons work. The DataContext.DoCommand binding usually works in different scenarios but not with these static commands.



asked 34 secs ago

Gábor

1,247






Binding to a static command from inside a DataTemplate

Aucun commentaire:

Enregistrer un commentaire