gpt4 book ai didi

c# - WPF - MVVM 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:32 24 4
gpt4 key购买 nike

我的应用程序有问题,尤其是 WPF MVVM 中的绑定(bind)。我创建了模型、 View 模型和 View ,这是我的代码的一部分(只有这与我的问题有关)当我单击 nemed 按钮时:PointUp 我想查看 Team1 点数。谁能告诉我我做错了什么?

查看

    <Window x:Class="Tabu.Game
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Tabu"
xmlns:vm="clr-namespace:Tabu.ViewModel"
mc:Ignorable="d"
Title="Game" Height="600" Width="900" Background="Beige">
<Window.DataContext>
<vm:TeamStatistic />
</Window.DataContext>
<Grid>
<Button x:Name="PointUp" Command="{Binding AddPoints }" Content="+"/>
<Label x:Name="PointsTeam1_label" Content="{Binding Team1.TeamPoints, UpdateSourceTrigger=PropertyChanged }"/>
</Grid>

模型

'

namespace Tabu.Model
{
public class Team
{
public bool IsTeamActive { get; set; }
public int TeamMiss { get; set; }
public int TeamPoints { get; set; }
public int TeamMistake { get; set; }
}
}
'

View 模型

namespace Tabu.ViewModel
{
class TeamStatistic : INotifyPropertyChanged

{
public Team Team1 = new Team();

public int TeamPoints
{
get { return TeamPoints; }
set { TeamPoints = value; OnPropertyChanged("TeamPoints"); }
}

public ICommand AddPoints
{
get { return new RelayCommand(() => Add_Points()); }
}

public void Add_Points()
{
Team1.TeamPoints++;
}

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(params string[] propsName)
{
if (PropertyChanged!=null)
{
foreach(string propName in propsName)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}

public class RelayCommand : ICommand
{
private readonly Func<bool> canExecute;
private readonly Action execute;

public RelayCommand(Action execute)
: this(execute, null) { }

public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null) throw new ArgumentNullException("execute");
this.execute = execute;
this.canExecute = canExecute;
}

public event EventHandler CanExecuteChanged
{
add { if (this.canExecute != null) CommandManager.RequerySuggested += value; }
remove { if (this.canExecute != null) CommandManager.RequerySuggested -= value; }
}

public Boolean CanExecute(object parameter) { return this.canExecute == null ? true : this.canExecute(); }
public void Execute(object parameter) { this.execute(); }
}
}

最佳答案

问题出在这里:

public int TeamPoints
{
get { return TeamPoints; } //should be Team1.TeamPoints
set { TeamPoints = value; OnPropertyChanged("TeamPoints"); } //should be Team1.TeamPoints
}

ViewModelTeamPoints 属性中,您从 ViewModel 返回并设置相同的属性 TeamPoints 但您应该从 Model(Team1)设置。您应该返回并设置 Team1.TeamPoints

public int TeamPoints
{
get { return Team1.TeamPoints; }
set { Team1.TeamPoints = value; OnPropertyChanged("TeamPoints"); }
}

Add_Points():

public void Add_Points()
{
Team1.TeamPoints++;
OnPropertyChanged("TeamPoints");
}

关于c# - WPF - MVVM 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com