0

I'm working on a project that uses Prism for its client software. I have a UserControl XAML file that looks something like this:

<UserControl x:Class="UserModule.Frontend.UserListView"
             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:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
  <Grid>
    <ListView ItemsSource="{Binding Users}"/>
  </Grid>
</UserControl>

The actual XAML is a bit longer, but my question is:

Rider shows me a warning at the binding of the list view's ItemsSource property. I have a ViewModel that Prism injects correctly and I can see that the list has been populated. However, at design time, I can't see if the property exists, if I don't check for myself. At the same time, I get a warning in the ViewModel class, that the public getter of Users could be removed.

Is there a way to get code completion to recognize the autowired ViewModel with Prism?

G_hi3
  • 588
  • 5
  • 22

1 Answers1

1

You need to define the d:DataContext for your view. And while you're at it, vote for the feature...

Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • If I define the `DataContext` property, isn't the auto wiring redundant? – G_hi3 Dec 01 '20 at 09:48
  • 1
    Yes and no - yes, because you're mentioning the type although it's resolved automatically, and no, because by specifying `d:DataContext` you're _not_ setting `DataContext` (note the `d:`) – Haukinger Dec 01 '20 at 11:03
  • Okay, to complete the solution: I added an interface called `IGroupListViewModel`, which my view model class implements. I also added a class called `GroupListViewModelStub` which implements the interface without any more logik than necessary. The XAML file references the Stub via `d:DataContext`, but prism passes it the actual view model at runtime. – G_hi3 Dec 01 '20 at 15:19
  • You can have a look at this answer https://stackoverflow.com/a/44458673/5758420 - with a `DesignTimeFactory` you can skip the additional interface. – Haukinger Dec 01 '20 at 18:39
  • Meanwhile, I figured out that it's enough to specify the interface in the `d:DataContext`, which means I only have to create the interface and a concrete implementation. – G_hi3 Dec 22 '20 at 12:53
  • By adding an interface, you add the burden of having to maintain two types (the actual view model and the interface) instead of one, and you lose the opportunity to set a `d:DataContext` with populated properties (and thus make actual use of the designer)... an interface here just doesn't make sense. – Haukinger Dec 22 '20 at 17:29