-1

This is my first question here :)

I create a listview columns form a textfile in Window_Loaded() After this I want to add items to this listview...

foreach (KeyValuePair<string, string> key in App.sysIni.monitorProperties)
            {

                GridViewColumn column = new GridViewColumn();
                column.Header = key.Value;
                column.Width = 70;
                GridViewControlMonitor.Columns.Add(column);
}    

After this I create listview items:

string[] rowItems = new string[list.Count];

for (int i = 1; i < list.Count; i++)
    {
    rowItems[i] = list[i];
    }
 var item = new ListViewItem { Content = rowItems};
 itemsList.Add(item);

And add this items in listview:

   RequestMonitorListV.Items.Add(item);

And my listview is populated with "String[] Array" not values...

Is possible to create a listview item with content and some kind of binding for that content?

var item = new ListViewItem { Content = rowItems, !!soomeBindingOptinos!!};

Can someone help me with this issue?

Thank you in advance

2
  • Possible duplicate of C# listView, how do I add items to columns 2, 3 and 4 etc? Commented Jun 12, 2017 at 15:33
  • 1
    Bind an ObservableCollection<string> to the Items of your Items property in XAML, then add the items while reading the text file. you may need to invoke the UI thread using the dispatcher... Commented Jun 12, 2017 at 15:46

2 Answers 2

1

You should bind the DisplayMemberBinding property of each GridViewColumn to a property of an object in the ListView's ItemsSource collection:

for (int i = 0; i<App.sysIni.monitorProperties.Count; ++i)
{
    KeyValuePair<string, string> key = App.sysIni.monitorProperties[i];
    GridViewColumn column = new GridViewColumn();
    column.Header = key.Value;
    column.Width = 70;
    column.DisplayMemberBinding = new Binding("[" + i + "]");
    GridViewControlMonitor.Columns.Add(column);
}

...and set the latter to an IEnumerable like for example a string[]:

string[] rowItems = new string[list.Count];
for (int i = 0; i<list.Count; i++)
{
    rowItems[i] = list[i];
}
RequestMonitorListV.ItemsSource = rowItems;
Sign up to request clarification or add additional context in comments.

Comments

0

I'm going to be focusing on the second section of your code, as that seems to be the portion that is actually causing trouble. Unfortunately there are a few details missing, so I'll be making some assumptions on intent, type, etc.

Here is the .xaml I'll be working against:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListView x:Name="myListView" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="498">
        <ListView.View>
            <GridView>
                <GridViewColumn/>
            </GridView>
        </ListView.View>
    </ListView>

</Grid>
</Window>

And here is my interpretation of that chunk of C#:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var myListViewVar = this.FindName("myListView") as ListView;

            string[] rowItems = new string[10];

            for (int i = 0; i < 10; i++)
            {
                rowItems[i] = i.ToString();
            }
            var item = new ListViewItem { Content = rowItems };//You are adding your string array to a singular element. It does not want it. It wants a nice string.
            var itemsList = new List<ListViewItem>();//You then wrap this singular item in a list. I'm inferring the type here, since the rest of your code does not reference it.
            itemsList.Add(item);

            myListViewVar.Items.Add(item);//You then add the ARRAY to the list of items. Again, this is not desired. See https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.items(v=vs.110).aspx for usage examples.
        }
    }
}

As explained above, WPF doesn't know how to stringify your array. Interpreting from what the rest of your code implies, what I think you want is:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var myListViewVar = this.FindName("myListView") as ListView;

            string[] rowItems = new string[10];

            for (int i = 0; i < 10; i++)
            {
                rowItems[i] = i.ToString();
            }

            for (int i = 0; i < 10; i++)
            {
                var item = new ListViewItem { Content = rowItems[i] };//Each ListViewItem holds an individual string rather than an array
                myListViewVar.Items.Add(item);//Then add that ListViewItem.
            }
        }
    }
}

Obviously this has a redundancy in the form of the second loop, but I wanted to keep with your code where you store everything outside of the loop.

As for data binding, I'd recommend looking at WPF Binding to local variable I don't use WPF too often, but it looks like what you want.

Hope this helps. Let me know if I've missed the mark on what you're intending.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.