2014-02-23 15 views
2

我在「細節」模式有一個列表視圖,看起來像:在ListView和變化值選擇子項目

################# 
Name # Property 
################# 
#Itm1 # Subitm1 
#Itm2 # Subitm2 
################# 

很簡單,但我遇到的問題是我無法選擇「Subitm1」運行時的列表。我可以選擇並突出顯示第一列中的每個項目,但單擊第二列中的任何項目都不會執行任何操作(我希望它會突出顯示第一列中的項目)。

具體而言,我試圖添加一個用戶能夠雙擊一個子項目並直接在列表視圖中更改其值的功能。有沒有我在這裏失蹤的具體設置?

+0

這是什麼平臺,wpf,winform,其他? – har07

+0

平臺是winform –

+0

雖然您可以使用ListView控件執行此操作,但設置起來很麻煩。你有沒有想過使用Grid控件呢? –

回答

7

如果要在單擊子項目時選擇整行,請嘗試使用FullRowSelect屬性ListView。 爲了處理一個子項雙擊,試試這個:

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e) 
{ 
    ListViewHitTestInfo hit = listView1.HitTest(e.Location); 
    // Use hit.Item 
    // Use hit.SubItem 
} 

如果你想允許最終用戶在列表視圖編輯子項的文字,恐怕最簡單的方法是使用Grid控件。另一種方法是嘗試這樣的事情:

private readonly TextBox txt = new TextBox { BorderStyle = BorderStyle.FixedSingle, Visible = false }; 

public Form1() 
{ 
    InitializeComponent(); 
    listView1.Controls.Add(txt); 
    listView1.FullRowSelect = true; 
    txt.Leave += (o, e) => txt.Visible = false; 
} 

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e) 
{ 
    ListViewHitTestInfo hit = listView1.HitTest(e.Location); 

    Rectangle rowBounds = hit.SubItem.Bounds; 
    Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label); 
    int leftMargin = labelBounds.Left - 1; 
    txt.Bounds = new Rectangle(rowBounds.Left + leftMargin, rowBounds.Top, rowBounds.Width - leftMargin - 1, rowBounds.Height); 
    txt.Text = hit.SubItem.Text; 
    txt.SelectAll(); 
    txt.Visible = true; 
    txt.Focus(); 
} 
+0

嗨,有無論如何確定正在編輯的項目屬於哪一列? –