我有一个名为MainWindowViewModel
的父视图模型,并且我想在其中显示另外两个名为TeamViewModel
的视图模型。在视图内部包含
<!--Team 1-->
<ContentControl Grid.Row="1" Grid.Column="0" x:Name="Team1"/>
<!--Team 2-->
<ContentControl Grid.Row="1" Grid.Column="1" x:Name="Team2" />
同时视图模型包含此
public TeamViewModel Team1 => new TeamViewModel();
public TeamViewModel Team2 => new TeamViewModel();
此视图正确显示,但是TeamViewModel
未绑定到这些视图,因为按钮根本没有响应。
预期的行为:https://chito.ge/5sYrgc6.mp4 实际:https://chito.ge/83Ne4oV.png
请注意,实际按钮没有禁用“-”按钮。点击所有按钮实际上没有任何作用。
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(已大大简化)
<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>