将ObservableCollection <class>字段绑定到ListBox DataTemplate

时间:2018-08-01 16:10:39

标签: c# data-binding observablecollection

我是一名刚刚完成暑期实习的学生,在学校开学之前,我带回了一个正在进行的项目。这个项目中有一个秒表,我宁愿使用绑定到ListBox的ObservableCollection作为我的拆分时间,而不是使用listbox.Items.Add()。当我添加到ObservableCollection时,ListBox UI不会更新。有人可以针对我错过的内容或做错的事情为我指明正确的方向吗?

我有我的TimeSplits课:

public class TimeSplits : INotifyPropertyChanged
{

    private int _hours;
    private int _minutes;
    private int _seconds;

    public int hours
    {
        get
        {
            return _hours;
        }
        set
        {
            _hours = value;
            NotifyPropertyChanged(hours);
        }
    }
    public int minutes
    {
        get
        {
            return _minutes;
        }
        set
        {
            _minutes = value;
            NotifyPropertyChanged(minutes);
        }
    }
    public int seconds
    {
        get
        {
            return _seconds;
        }
        set
        {
            _seconds = value;
            NotifyPropertyChanged(seconds);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

    public override string ToString()
    {
        return hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString();
    }
}

和我的页面中的ObservableCollection:

public partial class StopwatchPage : Page , INotifyPropertyChanged
{
...
    public ObservableCollection<TimeSplits> splits = new ObservableCollection<TimeSplits>();
...
    public StopwatchPage()
    {
        DataContext = this;
        InitializeComponent();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += new EventHandler(stopwatchTimer);
    }
...
    private void splitButton_Click(object sender, RoutedEventArgs e)
    {
        TimeSplits split = new TimeSplits();
        split.hours = Hours;
        split.minutes = Minutes;
        split.seconds = Seconds;
        splits.Add(split);
    }
...
}

和我的xaml:

<ListBox x:Name="newSplitListBox" HorizontalAlignment="Left" Margin="139,0,0,47" Width="185" Height="268" VerticalAlignment="Bottom" ItemsSource="{Binding splits}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding hours}"/>
                    <TextBlock Text="{Binding minutes}"/>
                    <TextBlock Text="{Binding seconds}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我确定这是一个很小的问题,因为我今年夏天才开始学习数据绑定。任何帮助是极大的赞赏!预先感谢。

1 个答案:

答案 0 :(得分:0)

您似乎在错误的位置输入了nameof()。当前代码的读取方式将始终发送“ propertyName”值作为已更改属性的名称,而不管实际更改了哪个属性。

尝试一下:

public int hours
{
    get
    {
        return _hours;
    }
    set
    {
        _hours = value;
        NotifyPropertyChanged();
    }
}

然后,在您的NotifyPropertyChanged()中,执行以下操作:

private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
    }
}

编辑:添加了以下修复程序:

此外,ObservableCollection必须是一个属性。更改此代码:

public ObservableCollection<TimeSplits> splits = new ObservableCollection<TimeSplits>();

对此:

public ObservableCollection<TimeSplits> Splits { get; set; } = new ObservableCollection<TimeSplits>();

我从Xamarin的ViewModel模板中学到了一个技巧,极大地帮助了我。这是它生成的用于处理可观察视图模型(类似于ObservableCollection)的代码。

    protected bool SetProperty<T>(ref T backingStore, T value,
        Action onChanged = null,
        [CallerMemberName]string propertyName = "")
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;

        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

然后,要使用此功能,只需将其添加到您的属性中即可:

private string _title = string.Empty;
public string Title
{
    get => _title;
    set => SetProperty(ref _title, value);
}