绑定到数组的特定元素不会更新

时间:2018-06-28 16:27:44

标签: c# wpf mvvm binding

我正在使用MVVM模式使用C#和.NET Framework 4.7开发WPF应用程序。

我的ViewModel类具有此属性:

public List<byte> BestIndividual
{
    get { return _bestIndividual; }
    set
    {
        _bestIndividual = value;

        RaisePropertyChangedEvent(nameof(BestIndividual));
    }
}

RaisePropertyChangedEvent是:

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChangedEvent(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

在视图中具有绑定的标签是:

<Label x:Name="c00" Content="{Binding BestIndividual[0]}" Margin="0" BorderThickness="2,2,0,0" BorderBrush="Black" />

我在事件处理程序中更新属性:

private void Algorithm_BestIndividualFound(object sender, EventArgs e)
{
    BestIndividualEventArgs best = (BestIndividualEventArgs)e;

    BestIndividual = best.BestIndividual;
}

我只能在Label上获得一个值,并且它永远都不会改变。

为什么不刷新标签内容?

更新:

我已将List<byte>更改为byte,但它仍然没有更新。新代码在这里:

private byte _bestIn;

public byte BestIn
{
    get { return _bestIn; }
    set
    {
        _bestIn = value;

        RaisePropertyChangedEvent(nameof(BestIn));
    }
}

我更新BestIn值的事件:

private void Algorithm_BestIndividualFound(object sender, EventArgs e)
{
    BestIndividualEventArgs best = (BestIndividualEventArgs)e;

    BestIn = best.BestIndividual[0];
}

还有XAML:

<Label x:Name="c00" Content="{Binding BestIn}" Margin="0" BorderThickness="2,2,0,0" BorderBrush="Black" />

0 个答案:

没有答案