gpt4 book ai didi

c# - 一击将ListView ObservableCollection内的所有项目添加到(WPF Mvvm)

转载 作者:行者123 更新时间:2023-12-03 10:55:34 24 4
gpt4 key购买 nike

我有一个带有两列Devicename和DeviceAddress的列表 View 。我为listview维护了一个observablecollection。我正在使用MVVM模式。

View :

<ListView Height="100" ItemsSource="{Binding I2CDeviceList}" SelectedItem="{Binding SelectedI2CAddress, Mode=TwoWay}" Name="I2cDeviceList">
<ListView.View>
<GridView>
<GridViewColumn Header="I2C Device" Width="190" DisplayMemberBinding="{Binding I2CDevName}" />
<GridViewColumn Header="I2C Device Address" Width="203" DisplayMemberBinding="{Binding I2CDeviceAddress}" />
</GridView>
</ListView.View>
</ListView>

I2CDevicename I2CDeviceAddress 都是我的模型类的一部分。

ViewModel:
public ObservableCollection<ModelClass> I2CDeviceList
{
get { return _I2CDeviceList; }
set
{
_I2CDeviceList = value;
NotifyPropertyChanged("I2CDeviceList");
}
}

分别在DeviceName和DeviceAddress中添加的项目为:
{ T("Other"), T("0x00")},
{ T("TI Codec(TLV320AIC3104)"), T("0x18")},
{ T("Chip ID GPIO(PCA9500)"),T("0x20")},
{ T("GPIO - power rail control(PCA9555DB)"),T("0x24")},
{ T("Digital Potentiometer(AD5252)"),T("0x2C")},
{ T("Audience chip(eSxxx)"),T("0x3E")},
{ T("Spartan 3A FPGA(XC3SD3400A)"),T("0x40")},.......

现在,在viewmodel类的构造函数中,我可以按以下方式在obs.Coll中添加项目:
public ObservableCollection<ModelClass> _I2CDeviceList = new ObservableCollection<ModelClass>()
{
new ModelClass() {I2CDevName = "Other", I2CDeviceAddress= "0x00"},
new ModelClass() {I2CDevName = "TI Codec", I2CDeviceAddress = "0x18"}, .........
};

但是添加15个项目是一项繁琐的工作,而我最终只有15个项目。我是否可以使用单个循环添加项目以避免很多语句?

最佳答案

因此,您可以为ModelClass构造一个构造函数,从而使创建起来更容易,并同时填写值。

public class ModelClass
{
public string I2CDevName { get; set; }
public string I2CDeviceAddress { get; set; }

public ModelClass(string DeviceName, string DeviceAddress)
{
this.I2CDevName = DeviceName;
this.I2CDeviceAddress = DeviceAddress;
}
}

这样可以减少一些代码,但是仍然会有一些输入:
public ObservableCollection<ModelClass> _I2CDeviceList = new ObservableCollection<ModelClass>()
{
new ModelClass("Other","0x00"),
new ModelClass("TI Codec", "0x18"), .......
};

或者,您可以在ModelClass中创建一个“factory”(静态)函数,该函数可以接收整个列表并返回整个ObservableCollection

因此,您将必须更改列表中的某些内容,但是如果将其更改为类似的内容(也许有人可以提出更好的想法来更好地使用列表,但是:
List<string[]> list = new List<string[]>() { new string[] {"Other", "0x00"},
new string[] {"Audience chip(eSxxx), "0x3E"}, ... };

然后将静态函数添加到ModelClass中:
public class ModelClass
{
public string I2CDevName { get; set; }
public string I2CDeviceAddress { get; set; }

public ModelClass(string DeviceName, string DeviceAddress)
{
this.I2CDevName = DeviceName;
this.I2CDeviceAddress = DeviceAddress;
}

//Collection Factory
public static ObservableCollection<ModelClass> CreateCollection(List<string[]> models)
{
ObservableCollection<ModelClass> tmpColl = new ObservableCollection<ModelClass>();
foreach (string[] s in models)
{
tmpColl.Add(new ModelClass(s[0],s[1]));
}
return tmpColl;
}
}

然后运行:
List<string[]> list = new List<string[]>() { new string[] {"Other",...},
...};
public ObservableCollection<ModelClass> _I2CDeviceList = ModelClass.CreateCollection(list);

多田...

关于c# - 一击将ListView ObservableCollection内的所有项目添加到(WPF Mvvm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12634541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com