User avatar
Scherub
Topic Author
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

[Unity] Binding to Commands

18 Sep 2014, 23:21

Hi,

i just came across another problem with data binding regarding commands. Up until now I usually tried to find the the current UserControl as I didn't explicitly set the datacontext in the code behind file.
<Button Content="Back" Command="{Binding GoBackCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MultiplayerMenuView}}}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="20" />
This works as expected. Due to another thread I was just thinking the whole concept through and was wondering why I didn't set the DataContext in the code behind. Afair the reason for that was that I didn't get everything to work that way.

So if I set the DataContext in the code behind it appearantly works for most things such as TextBoxes, TextBlocks, ListBoxes, etc. But for some weird reason it doesn't work for the command of the button.
<Button Content="Host Server" Command="{Binding HostServerCommand}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="20" />
So above XAML doesn't work. If I press the button nothing happens. I tried to search for the correct DataContext but this also doesn't seem to work. Unfortunately I can't find anything useful in the Noesis' build or play log.

Do you have any idea what could be wrong?
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: [Unity] Binding to Commands

19 Sep 2014, 10:09

Hi,

Our Commands sample binds the Button.Command this way and it works correctly. I even tried to remove the ViewModel from the xaml and set it later using a MonoBehavior and it works as expected too.

How do you expose the command property in your ViewModel?
How and where (in the logical tree) do you set the DataContext?

Let's see if we can figure out what is happening.

Who is online

Users browsing this forum: jsantos and 1 guest