I have a parent view model called MainWindowViewModel and I want to show two other view models inside of it called TeamViewModel. Inside of the view it contains

<!--Team 1-->
<ContentControl Grid.Row="1" Grid.Column="0" x:Name="Team1"/>

<!--Team 2-->
<ContentControl Grid.Row="1" Grid.Column="1" x:Name="Team2" />

Meanwhile the view model contains this

public TeamViewModel Team1 => new TeamViewModel();
public TeamViewModel Team2 => new TeamViewModel();

This views show properly, however the TeamViewModel is not binded to these views, as the buttons aren't responding at all.

Expected behavior: https://chito.ge/5sYrgc6.mp4 Actual: https://chito.ge/83Ne4oV.png

Notice that the actual doesn't have the "-" button disabled. Clicking on all the buttons do nothing in the actual.

TeamViewModel:

public class TeamViewModel : Screen, INotifyPropertyChanged
{
    public int Score { get; set; }
    [UsedImplicitly] public bool CanRemoveScoreTeam1 => Score > 0;

    [UsedImplicitly]
    public void RemoveScoreTeam1()
    {
        Score--;
    }

    [UsedImplicitly]
    public void AddScoreTeam1()
    {
        Score++;
    }

    [UsedImplicitly]
    public void ResetTeam()
    {
        Score = 0;
    }
}

TeamView (Heavily simplified)

<Grid>
    <!--Team Info-->
    <Viewbox>
        <Grid>
            <!--Team Image-->
            <Image />

            <!--Team Info-->
            <TextBox Text="New Team" />
        </Grid>
    </Viewbox>

    <!--Team score-->
    <Grid>
        <TextBlock HorizontalAlignment="Center"
                   Text="{Binding Path=Score, Mode=OneWay}" />

        <Grid>
            <Button Grid.Column="0" Margin="10" Content="-"
                    x:Name="RemoveScoreTeam" />
            <Button Grid.Column="1" Margin="10" Content=""
                    x:Name="ResetTeam" />
            <Button Grid.Column="2" Margin="10" Content="+"
                    x:Name="AddScoreTeam" />
        </Grid>
    </Grid>
</Grid>

0 Answers