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;
}
到目前爲止,您已經提出了9個問題,並沒有選擇任何答案。請接受一些答案。 – raym0nd
我已經接受3 ans – user644194
@ user644194 - 您通過點擊複選框大綱來接受它,而不是通過投票來接受它。 – CodeNaked