2013-05-12 22 views
3

我有一个WrapPanel其中包含一些图像(缩略图)。Loop虽然儿童元素,找东西,并访问其兄弟财产

当用户按左或右箭头键,我想显示下一个/上一个图像。

private void frmMain_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Right) 
    { 
     int j = 0; 
     foreach (Image child in WrapPanelPictures.Children) 
     { 
      if (child.Source == MainPic.Source) 
      { 
       MainPic.Source = WrapPanelPictures.Children[j + 1].Source; 
      } 
      j++; 
     } 
    } 
} 

另外我尝试了一种LINQ方法,但我是LINQ的初学者。

var imgfound = from r in WrapPanelPictures.Children.OfType<Image>() 
       where r.Source == MainPic.Source 
       select r; 
MessageBox.Show(imgfound.Source.ToString()); 

imgfound应该是一个列表,对不对?也许这就是为什么我无法访问Source属性。无论如何,这返回当前图像显示。我想要兄弟姐妹。

UPDATE: 那么我提出了一个解决方法,就像现在一样。但仍在等待一个合适的解决方案。

我创建了一个ListBox并将所有WrapPanel Childrens添加到它。然后使用SelectedItemSelectedIndex属性来选择上一个和下一个项目。

+0

你如何确定你的下一个和以前的起点(CurrentImage)? – WiiMaxx 2013-05-23 14:24:22

+0

@WiiMaxx“MainPic”的来源决定了当前显示的图片。按左右方向键可以改变当前的图像。我想我应该指出/链接缩略图图片和当前显示在一起的图片。由于我不能这样做(但),我必须遍历所有WrapPanel儿童。 – xperator 2013-05-23 14:38:17

回答

3

WrapPanelPictures.Children[j + 1].Source因为你试图访问的UIElementSource财产不存在不能正常工作。你应该投的UIElement到图像访问Source前:

MainPic.Source = (WrapPanelPictures.Children[j + 1] as Image).Source; 

我相信有更优雅的解决方案,以获得相同的结果。

+0

这是工作:)正是我在找什么。你说得对。应该有更好的解决方案来实现这一点。但这是我能想到的最接近的方法。试想一下,你有一个包含缩略图的WrapPanel,并且你想用键浏览它们。 – xperator 2013-05-23 15:20:55

0

使用方法FindVisualChildren。它遍历Visual Tree并找到你想要的控制。

这应该做的伎俩

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject 
{ 
if (depObj != null) 
{ 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
    { 
     DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 
     if (child != null && child is T) 
     { 
      yield return (T)child; 
     } 

     foreach (T childOfChild in FindVisualChildren<T>(child)) 
     { 
      yield return childOfChild; 
     } 
    } 
} 
} 

那么你枚举控件像这样

foreach (TextBlock tb in FindVisualChildren<TextBlock>(window)) 
{ 
    // do something with tb here 
} 
+0

我告诉过你,我想调用对象的(在本例中为tb)同级属性。不是本身。 – xperator 2013-05-12 08:58:43

0

不要声明j foreach循环中。否则,这将始终显示图像j=0这是WrapPanelPictures.Children[1]

private void frmMain_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Right) 
     { 
      int j = 0; 
      foreach (Image child in WrapPanelPictures.Children) 
      { 
       // int j = 0; 
       if (child.Source == MainPic.Source) 
       { 
        MainPic.Source = WrapPanelPictures.Children[j + 1].Source; 
        break; 
       } 
       j++; 
      } 
     } 
    } 
+0

好的我修好了那部分,但你错过了这一点。我在说'WrapPanelPictures.Children [j + 1] .Source'的Source属性不起作用。 – xperator 2013-05-12 08:47:39

1

你明白了你为什么不能访问Novitchi的Source属性。不过,我仍然想建议你重新考虑你的代码。

我看到它的方式是控制你的包装面板显示的内容。这意味着你应该能够存储诸如“所有图像”之类的东西以及所选择的索引在字段或属性中。您不必每次都对包装面板的子项进行解析并比较图像源,您应该在任何给定时间知道您选择的图像或索引是什么。

然后,该代码可能大致是这个样子:

private List<BitmapImage> _images; 
private int _selectedIndex; 

private void frmMain_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Right) 
    { 
     _selectedIndex = (_selectedIndex + 1) % _images.Count; 
     MainPic.Source = _images[_selectedIndex]; 
    } 
} 

如果你的UI是高度动态的(拖/下降的图像到画布面板或类似的东西),使用绑定到您的数据链接用户界面是要走的路。无论如何,我也强烈建议考虑像MVVM这样的ViewModel模式。

+0

你是对的。我有你的想法,创建一个图像列表,然后将它们添加到wrappanel中,然后将索引设置为0,并使用列表导航,....我希望我可以与你分享这个repution赏金:D – xperator 2013-05-24 16:53:50

+0

不用担心赏金,很高兴你发现它很有用。 – 2013-05-28 07:42:59