2014-01-24 65 views
0

我已經在搜索任何解決方案,但是我找不到一個解決方案。我在幾個討論我的問題的網站/論壇中發現了幾篇文章,但我想要一個非常簡單的解決方案。將listview子項複製到變量

while (SQLDataReader.Read()) 
       { 
        var view = new GridView(); 
        view.Columns.Add(new GridViewColumn { Header = "User", DisplayMemberBinding = new Binding("User") }); 
        view.Columns.Add(new GridViewColumn { Header = "hostNamedb", DisplayMemberBinding = new Binding("hostNamedb") }); 
        view.Columns.Add(new GridViewColumn { Header = "fullName", DisplayMemberBinding = new Binding("fullName") }); 
        view.Columns.Add(new GridViewColumn { Header = "location", DisplayMemberBinding = new Binding("location") }); 
        view.Columns.Add(new GridViewColumn { Header = "department", DisplayMemberBinding = new Binding("department") }); 
        view.Columns.Add(new GridViewColumn { Header = "position", DisplayMemberBinding = new Binding("postion") }); 
        view.Columns.Add(new GridViewColumn { Header = "message", DisplayMemberBinding = new Binding("message") }); 
        view.Columns.Add(new GridViewColumn { Header = "status", DisplayMemberBinding = new Binding("status") }); 
        view.Columns.Add(new GridViewColumn { Header = "ip", DisplayMemberBinding = new Binding("ip") }); 

        ListView2.View = view; 
        ListView2.Items.Add(new 
        { 
         User = SQLDataReader["User"].ToString(), 
         hostNamedb = SQLDataReader["hostNamedb"].ToString(), 
         fullName = SQLDataReader["fullName"].ToString(), 
         location = SQLDataReader["location"].ToString(), 
         department = SQLDataReader["department"].ToString(), 
         position = SQLDataReader["position"].ToString(), 
         message = SQLDataReader["message"].ToString(), 
         status = SQLDataReader["status"].ToString(), 
         ip = SQLDataReader["ip"].ToString(), 
        }); 
       } 


       userName_click = System.Convert.ToString(ListView2.Items[var2].User.Text.ToString()); 
       hostName_click = System.Convert.ToString(ListView2.Items[var2].hostNamedb.Text.ToString()); 
       fullName_form = System.Convert.ToString(ListView2.Items[var2].SubItems[2].Text.ToString()); 
       location_form = System.Convert.ToString(ListView2.Items[var2].SubItems[3].Text.ToString()); 
       department_form = System.Convert.ToString(ListView2.Items[var2].SubItems[4].Text.ToString()); 
       position_form = System.Convert.ToString(ListView2.Items[var2].SubItems[5].Text.ToString()); 
       message_Form = System.Convert.ToString(ListView2.Items[var2].SubItems[6].Text.ToString()); 
       status_form = System.Convert.ToString(ListView2.Items[var2].SubItems[7].Text.ToString()); 
       id_form = System.Convert.ToString(ListView2.Items[var2].SubItems[8].Text.ToString()); 
       userIP_click = System.Convert.ToString(ListView2.Items[var2].SubItems[9].Text.ToString()); 

基本上,這裏的代碼是這樣的:程序會從我的數據庫獲取數據,然後將它們添加到我的Listview2。如果我添加項目到listview2將工作?我現在不能運行程序,因爲我真的要做很多重新編碼。我正在從VB.NET遷移到C# WPF)。

打算在:每個變量,像userName_click將具有從 listview2-行VAR2與列中的數據0〜8

我試圖替換子項[N]從結合等用戶名 - > (新綁定(「用戶」)),但它沒有工作。順便說一下,這裏是我在listview2中的XAML代碼。

<ListView x:Name="ListView2" HorizontalAlignment="Left" Height="34" Margin="-10,446,-22,-50" VerticalAlignment="Top" Width="702" Grid.ColumnSpan="3" ClipToBounds="True"> 
      <ListView.View> 
       <GridView ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}"> 
        <l:FixedWidthColumn Header="username" DisplayMemberBinding="{Binding User}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="Host Name" DisplayMemberBinding="{Binding hostNamedb}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="fullname" DisplayMemberBinding="{Binding fullName}" FixedWidth="85"/> 
        <l:FixedWidthColumn Header="location" DisplayMemberBinding="{Binding location}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="department" DisplayMemberBinding="{Binding department}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="position" DisplayMemberBinding="{Binding position}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="message" DisplayMemberBinding="{Binding message}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="status" DisplayMemberBinding="{Binding status}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="id" DisplayMemberBinding="{Binding id}" FixedWidth="65"/> 
        <l:FixedWidthColumn Header="ip" DisplayMemberBinding="{Binding ip}" FixedWidth="65"/> 

       </GridView> 
      </ListView.View> 
     </ListView> 

回答

0

您的解決方案沒有遵循,但既然您知道綁定,我認爲您已經知道這一點。我不能幫你做你的現有代碼的工作,而是建議如下:使用可觀察集合與類似的用戶,hostNamedb字段保存您的數據庫對象...

public DbEntry(){ 
    user = SQLDataReader["User"].ToString(); 
    hostNamedb = SQLDataReader["hostNamedb"].ToString(); 
    fullName = SQLDataReader["fullName"].ToString(); 
    location = SQLDataReader["location"].ToString(); 
    ... 

}

這些都會填寫您的可觀察收藏。接下來,通過XAML中的ItemsSource綁定到該集合。

<ListView ItemsSource="{Binding DBObjectsCollection}"> 

填充數據的變量現在只有specifiying的「行」,從中獲取數據,並獲得正確的字段或屬性,例如索引的問題

userName_click = DBObjectsCollection[2].user; 

請注意,這段代碼不起作用。這只是一個基本的指針,可以幫助您解決WPF中的問題。關於WPF的信息已經很多,但如果出現任何問題,隨時都可以提出問題。