2017-06-12 97 views
-1

這是我第一次在這裏)的問題:)列表視圖中添加WPF ListView項創建動態

我創建一個列表視圖的列形成Window_Loaded(一個文本 這一點,我想將項目添加到列表視圖此之後...

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

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

在此之後,我創建的列表視圖項:

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

而且在列表視圖中添加此項目:

RequestMonitorListV.Items.Add(item); 

而我的列表視圖中填充「String []數組」沒有價值......

可以創建內容的列表視圖項和某種爲內容綁定?

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

有人可以幫我解決這個問題嗎?

預先感謝您

+0

[C#listView,如何將項目添加到列2,3和4等?](https:// stackoverflow。com/questions/473148/c-sharp-listview-how-do-i-add-items-to-columns-2-3-and-4-etc) – Sean

+1

將ObservableCollection 綁定到XAML中Your Items屬性的Items ,然後在閱讀文本文件時添加項目。您可能需要使用調度程序調用UI線程... – Chrisi

回答

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過於頻繁,但它看起來像你想要什麼。

希望這會有所幫助。讓我知道,如果我錯過了你打算的標記。

+0

您的示例非常好,並且可以正常工作。謝謝 –

0

你應該每個GridViewColumnDisplayMemberBinding屬性綁定到ListViewItemsSource集合中的對象的屬性:

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

...而後者設置爲IEnumerable例如像string[]

string[] rowItems = new string[list.Count]; 
for (int i = 0; i<list.Count; i++) 
{ 
    rowItems[i] = list[i]; 
} 
RequestMonitorListV.ItemsSource = rowItems; 
+0

謝謝,在我的項目中工作正常 –

+0

不客氣,但請記住接受答案:https://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on -堆棧溢出 – mm8