c# – 从csv生成列表到列表框
我正在尝试从我拥有的数据库中生成一个列表,并将其添加到
WPF C#中的列表框中,并使用LINQ.
这是我现在拥有的XAML: <ListView x:Name="ListBox" Margin="16,232,22,10.4" SelectionMode="Multiple"> <ListView.View> <GridView> <GridViewColumn Header="Nettstasjon" Width="100" DisplayMemberBinding="{Binding Path=Name}"/> <GridViewColumn Header="Omr?de" Width="100" DisplayMemberBinding="{Binding Path=Area}"/> <GridViewColumn Header="Radial" Width="100" DisplayMemberBinding="{Binding Path=Radial}"/> </GridView> </ListView.View> </ListView> 为了阅读数据库,我有这个代码隐藏.我创建了一个“TransformerStation”类来保存每行的数据. public class TransformerStation : INotifyPropertyChanged { public string Name { get; set; } public string Radial { get; set; } public string Area {get; set; } public event PropertyChangedEventHandler PropertyChanged; } 然后我创建了一个类来读取数据库 public IEnumerable<TransformerStation> ReadCSV(string fileName,string radial) { string[] lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName,".csv"),Encoding.UTF8); return lines.Select(line => { string[] data = line.Split(';'); foreach (var value in data) { if (data = radial) { return new TransformerStation(data[0],data[1],data[2]); } } }); } 我的数据库看起来像这样: N765;TANGEN;TANGEN L98 R2351;SPIKKESTAD;SPIKKESTAD K88 S622;KR?KSTAD;KR?KSTAD L812 S1318;KR?KSTAD;KR?KSTAD L812 我需要的是如下: 我有一个带有butttons的WPF,其内容等于我数据库中的第三列(F.eksKR?KSTADL812).我希望,当我按下按钮时,从我的数据库收集第一列中的所有项目,它们与第三列中的按钮内容相匹配,并在我上面创建的列表框中显示它们. 所以,如果我按下内容为“Kr?kstadL812”的按钮,那么我的列表就会显示出来 S622 S1318 对不起,我是C#和WPF的新手.所以我没有太多代码可以展示.但是我真的很感激我能得到的所有帮助,如果你解释一下代码中发生了什么,它真的会有所作为:)所以只是为了澄清,我基本上需要SELECT radial FROM数据库并从中生成一个列表. 编辑,我也对从csv数据库导入数据的其他方式持开放态度.我刚刚选了一个适合我的:) 解决方法
这里有很多问题.首先,您实际上没有正确实现INotifyPropertyChange支持.可能最简单的方法是将MVVM Lite添加到您的项目中(如果您使用NuGet,只需要一分钟)并将您的视图模型基于ViewModelBase:
public class TransformerStation : ViewModelBase { private string _Name; public string Name { get { return this._Name; } set { this._Name = value; RaisePropertyChanged(); } } private string _Radial; public string Radial { get { return this._Radial; } set { this._Radial = value; RaisePropertyChanged(); } } private string _Area; public string Area { get { return this._Area; } set { this._Area = value; RaisePropertyChanged(); } } public TransformerStation(string name,string radial,string area) { this.Name = name; this.Radial = radial; this.Area = area; } } 我注意到的第二件事是你在加载数据时似乎正在过滤数据,这表示每次用户按下按钮时都会重新加载数据.除非您拥有非常大量的数据,否则您可以将整个内容保存在内存中并在加载后对其进行过滤.如果您需要在运行时动态创建按钮,字典不仅会使过滤过程更快,而且还会为您提供数据集中所有径向的列表: public Dictionary<string,List<TransformerStation>> ReadCSV(string fileName) { string[] lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName,Encoding.UTF8); return lines.Select(line => { string[] data = line.Split(';'); return new TransformerStation(data[0],data[2]); }) .GroupBy(ts => ts.Radial) .ToDictionary(g => g.Key,g => g.ToList()); } 回到MainViewModel,您需要加载数据并将其存储在某个地方(即AllStations),您需要一个属性,如“SelectedRadial”,您为响应用户按下按钮而设置,另一个(“CurrentStations”)是AllStations的过滤版本: private Dictionary<string,List<TransformerStation>> _AllStations; public Dictionary<string,List<TransformerStation>> AllStations { get { return this._AllStations; } private set { this._AllStations = value; RaisePropertyChanged(); } } private string _SelectedRadial; public string SelectedRadial { get { return this._SelectedRadial; } set { this._SelectedRadial = value; RaisePropertyChanged(); this.CurrentStations = this.AllStations[value]; } } private List<TransformerStation> _CurrentStations; public List<TransformerStation> CurrentStations { get { return this._CurrentStations; } private set { this._CurrentStations = value; RaisePropertyChanged(() => this.CurrentStations); } } public ICommand RadialCommand {get { return new RelayCommand<string>(OnRadialCommand); }} private void OnRadialCommand(string radial) { this.SelectedRadial = radial; } public MainViewModel() { this.AllStations = ReadCSV(@"data.csv"); } 然后,只需要绑定一些最小的XAML来生成按钮(再次,这是可选的)并显示您的数据: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition /> </Grid.ColumnDefinitions> <ListBox ItemsSource="{Binding AllStations.Keys}"> <ListBox.ItemTemplate> <ItemContainerTemplate> <Button Command="{Binding Path=DataContext.RadialCommand,RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" CommandParameter="{Binding}"> <TextBlock Text="{Binding}" /> </Button> </ItemContainerTemplate> </ListBox.ItemTemplate> </ListBox> <ListView Grid.Column="1" ItemsSource="{Binding CurrentStations}" > <ListView.View> <GridView> <GridViewColumn Header="Nettstasjon" Width="100" DisplayMemberBinding="{Binding Path=Name}"/> <GridViewColumn Header="Omr?de" Width="100" DisplayMemberBinding="{Binding Path=Area}"/> <GridViewColumn Header="Radial" Width="100" DisplayMemberBinding="{Binding Path=Radial}"/> </GridView> </ListView.View> </ListView> </Grid> 结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 探究 Flex 组件的生命周期
- 深入NoSQL-读书笔记
- Swift 小贴士:语言的扩展和自定义
- sqlite 数据库增加 createdStamp, LastUpdatedStamp 列 (默
- 正则表达式 - 双引号内字符转星号/单词首字母转大写
- Oracle 12.2新特性掌上手册 - 第二卷 In-Memory增强
- 【原创】PostgreSQL 实现MySQL "insert ignore" 语
- VTR-to-Bitstream 2 FPGA Architecture File(.xml)
- /dev/mtd和/dev/mtdblock的区别
- 如何在C#中获取SharePoint术语的所有子项?