我无法使用INotifyPropertyChanged更新我的值

Tom*_*Tom 0 .net c# wpf mvvm inotifypropertychanged

所以我通过制作一个yatzee游戏来学习WPF,而我正在某个地方.但现在我无法理解为什么我的"当前滚动"计数器不会在视图中更新.我使骰子工作,滚动骰子按钮给我的骰子新值 - 并在视图中更新.但我的"当前滚动"变量不会.这就是我到目前为止所做的.

// CurrentRoll.cs
    public class CurrentRoll : INotifyPropertyChanged
{
    public int _roll;

    public int Roll
    {
        get { return _roll; }
        set
        {
            if (value != _roll)
            { 
            _roll = value;
            OnPropertyChanged("Roll");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后这是我的DiceModelView.当我按下"Roll Dices"按钮时,只要不加控制,我就可以在所有骰子上获得新值.我也增加_currentRoll变量,只要_currentRoll仍然小于3,只允许新的滚动.这个逻辑有效,变量有效,但它在View中的表示没有.我还将其初始值更改为其他值只是为了看到我的绑定有效,而且确实如此.

public class DiceModelView : INotifyPropertyChanged
{
    Die _die;
    public CurrentRoll _currentRoll;

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Die> myDices { get; set; }
    public ICommand RollCommand { get; set; }

    public DiceModelView()
    {
        myDices = new ObservableCollection<Die>()
        {
            new Die { Id = 0, Roll = 0, Checked = false },
            new Die { Id = 1, Roll = 0, Checked = false },
            new Die { Id = 2, Roll = 0, Checked = false },
            new Die { Id = 3, Roll = 0, Checked = false },
            new Die { Id = 4, Roll = 0, Checked = false }
        };
        _currentRoll = new CurrentRoll();
        _currentRoll._roll = 0;
        RollCommand = new Command (executeMethod, canexecuteMethod);
    }

    public bool canexecuteMethod(object parameter)
    {
        return true;
    }

    private void executeMethod(object parameter)
    {
        var r = new Random();
        if (_currentRoll._roll < 3)
        {
            foreach (Die d in myDices)
            {
                if (d.Checked == false)
                {
                    d.Roll = r.Next(1, 7);
                }
            }
        }
        _currentRoll._roll++;
    }

    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public Die die
    {
        get { return _die; }
        set { _die = value; }
    }

    public CurrentRoll currentRoll
    {
        get { return _currentRoll; }
        set { _currentRoll = value;
            NotifyPropertyChanged("currentRoll");
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的应用程序现在如何,在这个例子中我将_currentRoll._roll设置为111只是为了确保它有效. 由于上述值超过3,我没有掷骰子.

最后,这是我使用的XAML代码:

<Window.DataContext>
    <local:DiceModelView/>
</Window.DataContext>
<StackPanel>
    <TextBlock Text="{Binding currentRoll.Roll, UpdateSourceTrigger=PropertyChanged}"/>
    <ListView x:Name="DiceView" ItemsSource="{Binding myDices}" Width="500" HorizontalContentAlignment="Center" >
        <ListView.ItemTemplate>
            <DataTemplate >
                <CheckBox Content="{Binding Roll}" IsChecked="{Binding Checked}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Content="Roll the dices!" Command="{Binding RollCommand}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

Dan*_*rth 5

您需要更新Roll属性,而不是_role字段.

_roll 无论如何应该是私人的.

想一想:你在属性设置器中提升了属性更改事件,所以只有在设置属性值时才执行此操作.