3

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?

Alan
  • 7,875
  • 1
  • 28
  • 48
  • 2
    No I don't think you can. You can check this: http://stackoverflow.com/questions/1301119/silverlight-xaml-attribute-definition-order-matters – mlemay Mar 01 '13 at 19:45
  • 2
    think of your Xaml like a code block, you cant use variables before they are declared. – sa_ddam213 Mar 01 '13 at 23:52
  • @sa_ddam213 Yeah, I was hoping to think of it like a class, where I can use them regardless of where they are declared ;-) – Alan Mar 02 '13 at 06:38

3 Answers3

1

@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.

snowy hedgehog
  • 652
  • 1
  • 7
  • 23
  • I'll give it a second look – Alan Mar 02 '13 at 08:28
  • My test code at home is working now, but I'm sure I received an error before... bizarre.. I will check with the same code as before on Monday and get back to you – Alan Mar 02 '13 at 10:21
  • Alright, you're right. I have no idea why I was getting a binding error before, but I am receiving no such error and it is working fine. Thank you for being insistent! – Alan Mar 04 '13 at 15:12
0

Sure you can do everything in XAML, including such Binding at Window level. Show more code of your XAML/MyViewElement. Maybe we could tell you how to do it without errors. Also post your error here please.

snowy hedgehog
  • 652
  • 1
  • 7
  • 23
0

But, this fails because MyViewElement is not found because I assume it is declared later in the XAML.

To avoid that, you can use the following in the code behind instead of the constructor:

protected override void OnInitialized(EventArgs e)
{
    SomeCommand = new DelegateCommand(SomeExecuteMethod);

    InitializeComponent();
    base.OnInitialized(e);
}

This ensures the commands are already instantiated before you use them with:

Command="{Binding ElementName=MyUserCOntrol, Path=SomeCommand}"
RonnyR
  • 210
  • 1
  • 5