Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:34:03+00:00 2026-06-12T00:34:03+00:00

I have a listview with two columns Devicename and DeviceAddress. I have maintained an

  • 0

I have a listview with two columns Devicename and DeviceAddress. I have maintained an observablecollection for the listview. I am using MVVM pattern.

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>

Both I2CDevicename and I2CDeviceAddress are part of my model class.

ViewModel:

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

The items to be added inside DeviceName & DeviceAddress respectively are:

{ 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")},.......

Now In a constructor of the viewmodel class, I can add the items inside obs.Coll as follows:

public ObservableCollection<ModelClass> _I2CDeviceList = new ObservableCollection<ModelClass>()
        {                
            new ModelClass() {I2CDevName = "Other", I2CDeviceAddress= "0x00"},
            new ModelClass() {I2CDevName = "TI Codec", I2CDeviceAddress = "0x18"},  .........             
        };

but its a tedious job to add 15 items and I end up having 15 items statements. Is their a way i can add the items using a single loop to avoid many statements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:34:04+00:00Added an answer on June 12, 2026 at 12:34 am

    So either you make a constructor to the ModelClass which makes it easier for you to create, and at the same time fill in the values.

    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;
        }
    }
    

    This would reduce the code somewhat but there still will be some typing:

    public ObservableCollection<ModelClass> _I2CDeviceList = new ObservableCollection<ModelClass>()
    {                
        new ModelClass("Other","0x00"),
        new ModelClass("TI Codec", "0x18"), .......
    };
    

    Or you can make a “factory”, a static function in the ModelClass that takes a whole list and returns a whole ObservableCollection

    So you will have to change some in the list that you have, but if you change that to something like (maybe someone can come up with a better idea to use the list better, but:

    List<string[]> list = new List<string[]>() { new string[] {"Other", "0x00"},
                                                 new string[] {"Audience chip(eSxxx), "0x3E"}, ... };
    

    And then add the static function to the 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;
        }
    }
    

    And then just run:

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

    Tada…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview with two columns and I'm using a context menu to
I have ListView that uses a GridView to display several columns of data. Two
I have Listview on WinForm I need to have two columns but without headers,
I have a ListView that has two columns, one for a zone name and
I have to add two columns in a ListView . One column shows different
Hi I have a listview with two columns, Folder & Status . How do
I have a simple WPF ListView with two columns defined. By default when you
I have a list view with two columns- name and number. I want to
I have a listview with 3 coloumns. The first two columns has values and
I have a listview with two columns, one contains a textbox and the other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.