0

I have to perform a binding using ICommand, but it seems like the specific class I'm declaring my ICommand is not even triggered. I have defined following button in my AccView.xaml UserControl

<Button x:Name="buttonInit" Content="init" Height="32" Cursor="Hand" Command="{Binding initCommand}" HorizontalAlignment="Left" Margin="24,43,0,0" VerticalAlignment="Top" Width="156" Style="{DynamicResource RoundCornerButton}" />

I'm then using the specific class SetAccValues.cs:

public class GetAccValues : AccView
{

    public ICommand initCommand 
    {
        get { return new DelegateCommand<object>(initBluetooth, canInit); }
    }

    private async void initBluetooth(object context)
    {
        int serviceNumb = 1;
        await InitializeAsync(PerformAccOperations.Readings.None, serviceNumb);
        if (SensorOK && Initialized != null) Initialized(this);
    }

    private bool canInit(object context)
    {
        return true;
    }

}

But the problem is that the ICommand is not even triggered when I'm pressing the button. Where is the problem in this case ?.

user3812509
  • 131
  • 1
  • 3
  • 12
  • Is `GetAccValues` your view model? Its very small if it is. Is it set to the `DataContext` property of the view? – BradleyDotNET Nov 03 '14 at 18:05
  • Yes. I read somewhere that I could make it more simpler using DelegateCommand object than RelayCommand. That might be the problem perhaps. I did not know that the DataContext property has to be set to a specific class when using Commands. – user3812509 Nov 03 '14 at 18:10
  • How would you expect that the Button "finds" your class? The only thing the XAML can see is the {Binding initCommand}, how should the XAML find your "specific" class? You have to either set the DataContext/Source/RelativeSource. – Rand Random Nov 03 '14 at 18:11
  • DelegateCommand is fine; but you still have to set up the binding correctly. A command is bound just like anything else. – BradleyDotNET Nov 03 '14 at 18:11
  • What's wrong with `Button1.Click += new RoutedEventHandler((a, b) => somemethod())` why worry with binding? – Felix Castor Nov 03 '14 at 19:19
  • 1
    @Felix Castor - one reason to use binding rather than event handler is that your UI can be less coupled with the behaviour of your app. Let's say that you want to initialize a Bluetooth on button click. You have the event handler to do that. But what if you would like to initialize it on opening a dropdown list? Then you have to create a new event, hook the event to the control in XAML and rewrite your code behind. With `Command` you just change your XAML. ViewModel remains intact. Also if different teams work on different aspects of the app (UI, BL, DAL, etc.) it's easier to divide the work. – PiotrWolkowski Nov 03 '14 at 19:32

2 Answers2

0

The binding expression instructs the UI to look for properties on the form's DataContext.

To get the results you want, you'll need to do this in your constructor:

public AccView()
{
  this.DataContext = this;
}

That tells the runtime look at the AccView instance when it evaluates the binding expression.

NSFW
  • 557
  • 4
  • 12
0

Looks like you are missing a DataContext. In the comments you mentioned the GetAccValues is your ViewModel. You can set it in codebehind. Like in NSFW answer. Only set it to the instance of your ViewModel rather then to the View itself.

Or, you can do it in XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:AccTestApp"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:GetAccValues/>
</Window.DataContext>

After that, set a breakpoint in the getter of your initCommand to make sure that it is invoked. It should be initialized when the window is created.

If that works you can make the relation between the View and the ViewModel even less coupled by setting the data context in application resources and only use it as a static resource in your View. You can find the details of this approach here: https://stackoverflow.com/a/4590558/3330348

Community
  • 1
  • 1
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68