1

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.

wondra
  • 3,271
  • 3
  • 29
  • 48
  • If it help to find the solution, this happens only _first time_ the binding is evaluated and does not happen if the whole `KeyBinding` is defined in code rather than XAML. – wondra Jun 10 '19 at 10:10
  • It shouldn't be an issue then. These binding errors are harmless: https://stackoverflow.com/questions/47391020/cannot-find-source-for-binding-with-reference-relativesource-findancestor/47392478#47392478. – mm8 Jun 10 '19 at 14:43
  • @mm8 Thanks for confirmation on that matter. However the maintainability of project suffers greatly with list of "Do not fix this exact instance of error". Data Errors 2 are generally useful - usually happens when hosting context-sensitive controls in the wrong context so suppressing is not a real option neither. There is no option to suppress this exact instance, is there? – wondra Jun 10 '19 at 16:17
  • I don't think so. You may want to consider setting up the binding programmatically. Or simply ignore the warning. – mm8 Jun 11 '19 at 06:35

0 Answers0