3

We are writing a C# application in Visual Studio using WPF and MVVM.
In our View, we write interfaces in XAML, and then bind the GUI properties to functions in the ViewModel.

Here is a relevant snippet of the View code:

<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Width="124" HorizontalAlignment="Left" Content="New Exercise"  Command="{Binding OnNewExercise    }" IsEnabled="{Binding IsLoadEnabled       }" />
    <Button Width="124" HorizontalAlignment="Left" Content="Load Exercise" Command="{Binding OnLoadExercise   }" IsEnabled="{Binding IsLoadEnabled       }" />
    <Button Width="124" HorizontalAlignment="Left" Content="Save Exercise" Command="{Binding OnSaveExercise   }" IsEnabled="{Binding IsSaveEnabled       }" />
    <Button Width="124" HorizontalAlignment="Left" Content="Checkpoint"    Command="{Binding OnMakeCheckpoint }" IsEnabled="{Binding IsCheckpointEnabled }" />
</StackPanel>

The Bindings shown above are all public functions in the ViewModel.
Here is that snippet:

//Button bindings.  These functions are called on button clicks.
public ICommand OnNewExercise    { get { return _dashboardButtons.Exercise.NewCommand;    } }
public ICommand OnLoadExercise   { get { return _dashboardButtons.Exercise.LoadCommand;   } }
public ICommand OnSaveExercise   { get { return _dashboardButtons.Exercise.SaveCommand;   } }
public ICommand OnMakeCheckpoint { get { return _dashboardButtons.Checkpoint.MakeCommand; } }
public ICommand OnLoadCheckpoint { get { return _dashboardButtons.Checkpoint.LoadCommand; } }

Problem:
When someone renames a function in the ViewModel,
the View still builds, and it causes our interfaces to stop working.

How can I guarantee at compile time that the bindings in the View match the existing functions in the corresponding ViewModel? It would be great if the View.xaml was able to cause a compilation failure.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • If you set DataContext via XAML to the resource, e.g , does it show errors in Error Window? – Artavazd Balayan Oct 06 '16 at 16:20
  • Have you tried this: http://stackoverflow.com/questions/29399157/how-do-i-specify-datacontext-viewmodel-type-to-get-design-time-binding-checkin?rq=1 – Versatile Oct 06 '16 at 16:38
  • Short of devising a unit test which checks the available methods against the definitions in the XAML, I'm not sure how you would do this. I've run into similar issues with MVC in the past and ended up reflecting the classes to cross check the methods. I'd be intrigued to see if there is a better approach. – Alex Oct 06 '16 at 20:35
  • Gah what a good question! I suppose you might be able to listen to `DependencyProperty.UnsetValue` since it's what fires off `FallbackValue` on the dependency property but either way I'm not sure how to trigger a compile error. +1 dude. – Chris W. Oct 06 '16 at 22:34
  • 1
    Did you try using this d:DataContext="{d:DesignInstance myApp:MyViewModel, IsDesignTimeCreatable=false} ? – atomaras Nov 22 '16 at 04:22

0 Answers0