2011-07-27 80 views
0

我想顯示列表框項目值到msg框中。我使用它的click事件只顯示datarowview對象而不是實際的字符串值。將值從listboxitem轉換爲字符串

void bt_click(object sender, RoutedEventArgs e) 
{ 
    // MenuItem originalItem = (MenuItem)sender; 
    // MessageBox.Show(string.Format("clicked from \"{0}\"", originalItem.Name)); 
    MenuItem clickedMenuItem = sender as MenuItem; 
    ContextMenu contextMenu = clickedMenuItem.Parent as ContextMenu; 
    DockPanel dockPanel = contextMenu.PlacementTarget as DockPanel; 
    ListBoxItem listBoxItem = GetVisualParent<ListBoxItem>(dockPanel); 
    MessageBox.Show(listBoxItem); 
} 
public static T GetVisualParent<T>(object childObject) where T : Visual 
{ 
    DependencyObject child = childObject as DependencyObject;  
    // iteratively traverse the visual tree 
    while ((child != null) && !(child is T)) 
    {  child = VisualTreeHelper.GetParent(child); 
    } 
    return child as T; 
} 
+3

到目前爲止,您已經提出了9個問題,並沒有選擇任何答案。請接受一些答案。 – raym0nd

+0

我已經接受3 ans – user644194

+0

@ user644194 - 您通過點擊複選框大綱來接受它,而不是通過投票來接受它。 – CodeNaked

回答

2

嘗試ListBoxItem.Content屬性。您必須將其轉換爲字符串才能在Messagebox中輸出。

+0

我使用了它bt它stil給datarowview – user644194