2017-03-25 18 views
0

我有一個列表視圖表see the picture in the link我如何在一個特定的行中的列表視圖(WPF)更新特定的列編程

我想要做的就是以編程方式更改在y行的列X。 我正在使用wpf和一個listview與下面的xaml代碼。

`<ListView x:Name="listView1" Height="153" Width="444"> 
       <ListView.View> 
        <GridView> 
         <GridViewColumn Header ="Code" Width="148"></GridViewColumn> 
         <GridViewColumn Header ="Name" Width="148"></GridViewColumn> 
         <GridViewColumn Header ="Country" Width="148"></GridViewColumn> 
        </GridView> 
       </ListView.View> 
      </ListView> 

` 我想要做什麼是編程方式更改y行的列X。 類似這樣的 listview.Items [x] .ColumnIndex [y] =「我的價值」; 我需要傳遞一個字符串值,我不在那裏使用數據綁定。

+0

如果我理解的很好,這就是你想要的:http://stackoverflow.com/questions/29483660/how-to-transpose-matrix我沒有使用WPF,但如果這是你想要的,你可以在你的問題有確切的答案之前繼續你的研究 –

回答

0

圖片和您的XAML標記不完全匹配。我強烈推薦閱讀關於ListViews如何在這裏工作的解釋:http://www.wpf-tutorial.com/listview-control/simple-listview/

當更改ListView中顯示的內容時,修改ItemsSource及其內容,而不是視覺「單元格」。這最好用數據綁定完成。

比方說,你的ListView的設置是這樣的:

XAML:

<ListView x:Name="myListView"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn DisplayMemberBinding="{Binding Code}" Header="Code"/> 
      <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/> 
      <GridViewColumn DisplayMemberBinding="{Binding Country}" Header="Country"/> 
     </GridView> 
    </ListView.View> 
</ListView> 

C#:

public class MyInfo 
{ 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public string Country { get; set; } 
} 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     ... 

     ObservableCollection<MyInfo> items = new ObservableCollection<MyInfo>(); 
     items.Add(new MyInfo() { Code = "mycode1", Name = "myname1", Country = "mycountry1" }); 
     items.Add(new MyInfo() { Code = "mycode2", Name = "myname2", Country = "mycountry2" }); 
     items.Add(new MyInfo() { Code = "mycode3", Name = "myname3", Country = "mycountry3" }); 

     myListView.ItemsSource = items; 
    } 
} 


要在第二列中更改名稱值,將使用:

items[1].Name = "mynewname2"; 
相關問題