Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XAML is it possible to use an ElementName binding before the element it refers to is declared?

Tags:

wpf

I have a UserControl with some InputBindings. I wanted to make one of the input bindings (arrow key press) execute a command on a GUI control in my UserControl . So

e.g.

<UserControl.InputBindings>
    <KeyBinding Key="Up" Command="{Binding ElementName=MyViewElement, Path=MoveUpCommand}"/>
    <KeyBinding Key="Down" Command="{Binding ElementName=MyViewElement, Path=MoveDownCommand}"/>
</UserControl.InputBindings>

But, this fails because MyViewElement is not found because I assume it is declared later in the XAML. If I move my InputBindings to the end of the XAML file everything works as intended.

I kinda prefer my InputBindings to be at the top, is it possible to make it ignore the declaration order?

like image 769
Alan Avatar asked Dec 10 '25 22:12

Alan


1 Answers

@Stewbob What are you talking about?

Take a look at this: http://msdn.microsoft.com/en-us/library/ms752308.aspx

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>

According to what you said this should never work properly but it does:

<StackPanel>
  <Menu>
    <MenuItem Command="ApplicationCommands.Paste"
              CommandTarget="{Binding ElementName=mainTextBox}" />
  </Menu>
  <TextBox Name="mainTextBox"/>
</StackPanel>

From what you said the binding comes first also it will be executed first and therefore the binding to mainTextBox should never work. Thats very not true.

like image 62
snowy hedgehog Avatar answered Dec 12 '25 17:12

snowy hedgehog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!