我声明了一个类用作数据的动态绑定,其中定义了一个string类型的list,想动态绑定到listbox或者其他控件,但在测试的时候发现添加成员到string集合无反应
这块我不是很懂,是绑定哪里出错了嘛
这是我用来动态绑定的类,其中的string类型集合
class BindingProperty : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private List<string> _readDataTextBlock = new List<string>;
public List<string> ReadDataTextBlock
{
get { return _readDataTextBlock; }
set { _readDataTextBlock = value;OnPropertyChanged(nameof(ReadDataTextBlock)); }
}
}
这是后端调用的代码,我用“0000”做测试
public partial class MainWindow : Window
{
private BindingProperty _bindingProperty = new BindingProperty();
public MainWindow()
{
this.DataContext = _bindingProperty;
InitializeComponent();
}
private void ButtonReadByte_Click(object sender, RoutedEventArgs e)
{
_bindingProperty.ReadDataTextBlock.Add("0000");
}
}
这是xaml里面的binding设置
<ListBox x:Name="ListBoxReadResult" Canvas.Left="93" Canvas.Top="30" Width="180" Height="144" Background="White"
ItemsSource="{Binding Path=ReadDataTextBlock}"/>
点击click时监控string集合是有添加的,但是listbox始终无显示,如果在这基础上改如何改动最小