canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
988 stars 143 forks source link

Issue with datcontext/binding #155

Closed micah686 closed 3 years ago

micah686 commented 4 years ago

I'm having trouble setting up certain types of XAML Binding, so that I can right click on the property/Command, and go to the relevant section on the ViewModel. For example:

<UserControl....
xmlns:viewModels="clr-namespace:VnManager.ViewModels"
mc:Ignorable="d" 
 d:DataContext="{d:DesignInstance viewModels:RootViewModel}"
>
<Grid Grid.Column="0" x:Name="GrdSideBar">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="160"/>
                        <RowDefinition Height="90*"/>
                        <RowDefinition Height="40"/>
                    </Grid.RowDefinitions>
                    <ContentControl Grid.Row="0" s:View.Model="{Binding LastPlayedPage}"/>
                    <ContentControl Grid.Row="1" s:View.Model="{Binding CategoryListPage}"/>
                    <ContentControl Grid.Row="2" s:View.Model="{Binding AddGamePage}"/>
                </Grid>
</UserControl>
public LastPlayedViewModel LastPlayedPage { get; set; }
public CategoryListViewModel CategoryListPage { get; set; }
public AddGameButtonViewModel AddGamePage { get; set; }

With the code above, I get a :

Cannot resolve property 'LastPlayedPage ' in data context of type MyAppName.ViewModels.RootViewModel.

Is there also a way to have the methods used in the s:Action _____ show up in the view model as being used? So something like Command="{s:Action SubmitAsync} would have the SubmitAsync command in the ViewModel shown as being referenced? Or does that go against the design principles of Stylet?

canton7 commented 4 years ago

Those properties you posted, are they from RootViewModel? WPF doesn't think that RootViewModel has a property called LastPlayedPage (that trailing space might or might not be significant, I'm not sure). This is a WPF issue anyway, not a Stylet one.

Is there also a way to have the methods used in the s:Action _____ show up in the view model as being used?

I don't know of any. Let me know if you find one!

micah686 commented 3 years ago

No, it's from a custom UserControl that I've made, not the RootViewModel.

canton7 commented 3 years ago

The DataContext at that point is RootViewModel though, as you can see from the binding error. From the small snippet you posted it's not clear why you think the DataContext should be anything else.

Can you post a complete minimal reproducible sample, so I can see what you're actually trying to do? At the moment there simply isn't enough information to give any help.

micah686 commented 3 years ago

Nevermind, I'm blind. I was using the RootViewModel as the datacontext, instead of the associated VM class.