WPF在动态创建的列表视图中添加列表视图项

时间:2017-06-12 15:23:26

标签: c# wpf visual-studio data-binding

这是我的第一个问题:)

我在Window_Loaded()中创建一个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);
}    

在此之后我创建listview项目:

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

并在listview中添加此项:

   RequestMonitorListV.Items.Add(item);

我的listview中填充了&#34; String [] Array&#34;不是价值......

是否可以创建包含内容和该内容的某种绑定的列表视图项?

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

有人可以帮我解决这个问题吗?

提前谢谢

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

我将专注于代码的第二部分,因为这似乎是实际造成麻烦的部分。不幸的是,缺少一些细节,所以我将对意图,类型等做出一些假设。

这是我将要反对的.xaml:

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

以下是我对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.
        }
    }
}

如上所述,WPF不知道如何对数组进行字符串化。从其他代码的含义中解释出来,我认为你想要的是:

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

显然,这有第二个循环形式的冗余,但我想保留你的代码,在那里存储循环之外的所有内容。

至于数据绑定,我建议查看WPF Binding to local variable 我不经常使用WPF,但它看起来像你想要的。

希望这会有所帮助。如果我错过了你想要的标记,请告诉我。