0
我有一個自定義列表框。一些藏品的商品有「名稱」,「文字」,「圖片」和「網址」字段。其他可能有其他字段(使用模板選擇器)。所以,如果item具有字段「name」,「text」,「url」和「image」 - 它將在列表框中顯示爲2個文本塊和1個圖像。當我點擊圖像時 - 程序必須打開新窗口,打開webBrowser並轉到項目屬性「url」中的url。我知道如何將信息從一個頁面傳輸到其他頁面,但我無法理解如何從項目獲取「url」。我試過如何在事件「點擊」後獲取列表框項目的屬性
private void Video_Tap(object sender, GestureEventArgs e) // event when tap on the image
{
New tmp = ((sender as ListBox).SelectedItem as New); // New - is the type of collection's item
string vid = tmp.Video.url; // Video has fields "image" and "url"
string destination = string.Format("/Video_Page.xaml?uri={0}", vid);
NavigationService.Navigate(new Uri(destination, UriKind.Relative));
}
但發件人有圖像類型。
嘗試類似於'New tmp =((sender as Image).Parent.',但我無法看到我的項目的任何屬性,只有默認的方法 – eg01st
您必須將父項轉換爲其父項的任何類型(即Panel ,StackPanel等),因爲'FrameworkElement.Parent'的返回類型是一個'DependencyObject'。 – ceyko
另外,我應該提到你應該通過'(Image)sender'而不是'as'來推理。因爲失敗的行爲,它會立即拋出一個轉換異常*,如果'as'將返回null,如果它不是正確的類型,這可能導致在該行的某個地方產生'NullReferenceException'。 – ceyko