我无法使用INotifyPropertyChanged更新我的值

时间:2017-09-18 09:10:44

标签: c# .net wpf mvvm inotifypropertychanged

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

// 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));
        }
    }
}

然后这是我的DiceModelView。当我点击我的" Roll Dices"按钮我可以在所有骰子上获得新值,只要它们不受控制即可。我也增加_currentRoll变量,只要_currentRoll仍然小于3,只允许新的滚动。这个逻辑工作,变量有效,但它在视图中的表示不起作用。我也将其初始值更改为其他值只是为了看到我的绑定有效,而且确实如此。

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");
        }
    }

How my application is now, in this example I set _currentRoll._roll to 111 just to be sure that it works. I've not rolled any dices as the aforementioned value is more than 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>

1 个答案:

答案 0 :(得分:5)

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

无论如何,

_roll应该是私密的。

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