2012-05-03 34 views
1

我需要在鼠標上僅在silverlight 5上顯示圖像5. 任何人都可以幫助我。 給我任何的想法如何實現的呢?如何在Silverlight數據網格中的鼠標上顯示圖像

<sdk:DataGridTemplateColumn x:Name="colDeleteContent" IsReadOnly="True" Header="Delete Content" Width="100" CanUserResize="False"> 
<sdk:DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
    <StackPanel x:Name="spDeleteContent" VerticalAlignment="Center" Margin="10,0,0,0" Width="20" Height="20" HorizontalAlignment="Center" Orientation="Vertical">                
    <Image x:Name="imgDeleteContent" Source="Assets/Images/close.png" Height="15" Width="15" Margin="0" MouseLeftButtonDown="imgDeleteContent_MouseLeftButtonDown" Cursor="Hand"/>                
    </StackPanel> 
    </DataTemplate> 
</sdk:DataGridTemplateColumn.CellTemplate> 
</sdk:DataGridTemplateColumn> 

霓虹燈

回答

0

的方法有很多,聚焦狀態設爲您的圖像可見性可見和FocusLeft集基本上是你的主要元素倒塌。

但我看到它在您的示例DataTemplate上。

所以有一些我想象的方式。

1)在DataTemplate中創建一個新的組件,而不是元素如

namespace ProjectBus 
{ 

    public class StackPanelHasHiddenImage : Control 
    { 
     //You may don't need dependency property 
     //It supports bindability 
     #region dependency property 
     public static Image GetMyProperty(DependencyObject obj) 
     { 
      return (Image)obj.GetValue(ImageProperty); 
     } 

     public static void SetMyProperty(DependencyObject obj, Image value) 
     { 
      obj.SetValue(ImageProperty, value); 
     } 

     // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty ImageProperty = 
      DependencyProperty.RegisterAttached("Image", typeof(Image), typeof(StackPanelHasHiddenImage), new System.Windows.PropertyMetadata(null)); 
     #endregion 

     public Image Image 
     { 
      get; 
      set; 
     } 

     protected override void OnGotFocus(RoutedEventArgs e) 
     { 
      Image.Visibility = Visibility.Visible; 
      base.OnGotFocus(e); 
     } 
     protected override void OnLostFocus(RoutedEventArgs e) 
     { 
      Image.Visibility = Visibility.Collapsed; 
      base.OnLostFocus(e); 
     } 
    } 
} 

然後在你的XAML中使用像

<DataTemplate> 
    <local:StackPanelHasHiddenImage Image="/ProjectBus;component/blabal.png"/> 
</DataTemplate> 

2)使用GotoStateAction行爲 http://msdn.microsoft.com/en-us/library/ff723953%28v=expression.40%29.aspx但我看到它在一個DataTemplate並使用它可能並不容易。 3)MainElement.FinChildByType < StackPanel>()。FirstOrDefault()不爲null,然後將焦點和unfocus處理程序添加到代碼隱藏的此元素。但這是我主要避免使用的一種方法。

它有點困難,因爲它在一個模板中,所以您的模板中的命名對象無法在代碼隱藏中看到。

Hope help

+0

感謝您的回覆。我會嘗試你的代碼。 – Arun

+0

我試過你的代碼。什麼是的命名空間。請幫助... – Arun

+0

我編輯的部分1)我想你的類StackPanelHasHiddenImage類的名稱空間是你的XAML中的ProjectBus只需添加xmlns:local =「clr-namespace:ProjectBus」其中定義了xmlns。本地不是保留字這是我編造的。您可以在名稱空間(如ProjectBus.Controls)中收集控件,然後在聲明式編碼的xaml中確定使用之前,請添加xmlns:controls:「clr-namespace:ProjectBus.Controls」。 –

相關問題