Search code examples
c#wpfmvvmbinding

WPF bind Button with Command but cannot trigger


I am currently learning MVVM, and I'm having this problem. So basically I have a main view, such as

<Window x:Class="ManufacturingToolV2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:views="clr-namespace:ManufacturingToolV2.View"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Grid DataContext="{StaticResource viewModel}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <views:BurnInView />      
    </Grid>

And the "BurnInView" is a separate view that has a button:

<UserControl x:Class="ManufacturingToolV2.View.BurnInView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:viewmodel="clr-namespace:ManufacturingToolV2.ViewModel"
         xmlns:converter="clr-namespace:ManufacturingToolV2.Converters"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800"
         >
<UserControl.Resources>
    <viewmodel:BurnInTestViewModel x:Key="viewModel"/>
    <converter:BoolToStringConverter x:Key="boolToStringConverter" />
</UserControl.Resources>
<Grid DataContext="{StaticResource viewModel}" Background="White">               
            <Button Content="Start Test" HorizontalAlignment="Stretch" Margin="10,86,10,0" VerticalAlignment="Top" Command="{Binding StartTestCommand, Mode=OneWay}">
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding TestRunning}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding TestRunning}" Value="False">
                                <Setter Property="IsEnabled" Value="True"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>>
</Grid>

My "BurnInView" is binded to a view model such that:

public class BurnInTestViewModel : ViewModelBase
{
    public ICommand StartTestCommand;
    public BurnInTestViewModel()
    {
        StartTestCommand = new DelegateCommand((time) => { StartBurnIn(3000); }, canExecute => { return !TestRunning; });
    }

    void StartBurnIn(int time)
    {
        cycles = 0;
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(time);
        timer.Tick += (seder, e) => { cycles++; };
        timer.Start();
    }
}

But now the when I click the button, nothing happens, it doesn't even hit the beginning of the function...

Can anyone tell me what's going wrong?


Solution

  • You just forgot to declare your Command as a property:

    public ICommand StartTestCommand { get; set; }
    

    To trace this type of errors, I advise you to use the tab "XAML Binding Failure" during your Debug. It can help to quickly understand why a Binding does not work, in your example:

    enter image description here