2014-10-06 66 views
0

我在WPF中有5个用于缩略图视图的图像控件和1个用于放大视图的图像控件。将一个控件的图像源设置为wpf中另一个控件的图像源

我想当鼠标被采取任何上述5个的图像的EnlargedImageControl源属性设置到5缩略图的源中的一个的源极上悬停

<Image Source="{Binding DataContext.PassportSizeImage, ElementName=Root}"/> 
<Image Source="{Binding DataContext.PassportImage, ElementName=Root}"/> 
<Image Source="{Binding DataContext.VisaImage, ElementName=Root}"/> 
<Image Source="{Binding DataContext.SSNImage, ElementName=Root}"/> 
<Image Source="{Binding DataContext.BarCodeImage, ElementName=Root}"> 

,我需要设置的该源属性图像下面的ZoomImage的源属性。

--Set ZoomImage source equal to image last hovered above. 
<Image Name="ZoomImage" Source="{Binding DataContext.BarCodeImage, ElementName=Root}"> 
+0

能否请您详细些吗? – pushpraj 2014-10-06 02:46:05

回答

0

 我没有足够的英语来解释细节。
 我只希望你能理解我说的话。无论如何,你应该只使用绑定的xaml?我认为他们是简单的方法,如果你使用事件处理程序。

 您可以使用事件处理程序,如

private void Image_MouseEnter(object sender, RoutedEventArgs e) 
{ 
    // TODO: 
    // Set Image. 
} 

private void Image_MouseLeave(object sender, RoutedEventArgs e) 
{ 
    // TODO: 
    // Reset image. 
} 


 但它可能是不安全的线程,所以我建议的IsMouseDirectlyOverChanged事件。
 这是DependencyPropertyChangedEvent,你可以使用它像如下,

<!-- *.xaml file --> 
<UserControl x:Class=... ... ... ... 
      ... ... ... ... ... ... 
      x:Name="FaverName" 
      Width="..." Height="..."> 
    <!-- 
    ... 
    --> 
    <StackPanel x:Name="StackPanel_Images"> 
    <Image Source="{Binding ElementName=Root, Path=DataContext.PassportSizeImage}"/> 
    <Image Source="{Binding ElementName=Root, Path=DataContext.PassportImage}"/> 
    <Image Source="{Binding ElementName=Root, Path=DataContext.VisaImage}"/> 
    <Image Source="{Binding ElementName=Root, Path=DataContext.SSNImage}"/> 
    <Image Source="{Binding ElementName=Root, Path=DataContext.BarCodeImage}"> 
    </StackPanel> 
    <!-- 
    ... 
    --> 
    <!-- This control is target. --> 
    <Image Source="{Binding ElementName="FaverName", Path="ZoomedImageSource}"/> 
</UserControl> 


,然后后面的代码。

// .cs file 
// DependencyProperty for binding. 
static public readonly DependencyProperty ZoomedImageSourceProperty = DependencyProperty.Register("ZoomedImageSource", typeof(ImageSource), typeof(YourUserControl)); 
public ImageSource ZoomedImageSource 
{ 
    get { return GetValue(ZoomedImageSourceProperty) as ImageSource; } 
    set { SetValue(ZoomedImageSourceProperty, value); } 
} 

public YourUserControl() 
{ 
    // ... 

    // Chain new event. 
    foreach(Image item in StackPanel_Images.Children) 
    { 
    item.IsMouseDirectlyOverChanged += (s, e)=> 
    { 
     if((bool)e.NewValue) 
     ZoomedImageSource = (s as Image).Source; 
    } 
    } 
} 
// ...