I have app, where some controls can 'publish' certain keybinding which in turn appears in context help, for example a button publishes F5 a keybinding:
<Button Name="PublishKeybinding" Content="Publish my keybindings!">
<Button.InputBindings>
<KeyBinding Command="{Binding TestCommand}" Gesture="F5" />
</Button.InputBindings>
</Button>
And there is context help showing all published Commands as clickable list:
<ListView Name="PublicKeybindingsList" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Gesture}">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding Command}" Gesture="LeftClick"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
both(published and context) instances of the command work and execute correctly, however there is a binding error logged when PublicKeybindingsList
item's binding is being evaluated:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=TestCommand; DataItem='TestVM' (HashCode=47530006); target element is 'KeyBinding' (HashCode=53182860); target property is 'Command' (type 'ICommand')
How can I 'fix' the binding error?
For completeness codebehind used to reproduce the problem:
public class SomeCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public void Execute(object parameter)
{
MessageBox.Show("Success!");
}
}
public class TestVM
{
public ICommand TestCommand { get; } = new SomeCommand();
}
public partial class MainWindow : Window
{
public ObservableCollection<KeyBinding> AllCommands { get; } = new ObservableCollection<KeyBinding>();
public MainWindow()
{
InitializeComponent();
PublishKeybinding.DataContext = new TestVM();
PublicKeybindingsList.DataContext = AllCommands;
foreach (var cmd in PublishKeybinding.InputBindings.OfType<KeyBinding>())
{
AllCommands.Add(cmd);
}
}
}
Note that the code sample is "Minimal, Reproducible Example" and displayed values and/or keybindings does not make much sense.