1

我只需要在silverlight 5中在鼠标上显示图像。任何人都可以帮助我。给我任何想法如何实现它...

<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>

4

1 回答 1

0

有很多方法,OnFocus 设置你的图像 Visibility Visible 和 FocusLeft 设置 Collapsed 基本上你的主要元素。

但我看到它在您样本的 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() 不为空,然后将焦点和取消焦点处理程序添加到代码隐藏中的此元素。但这是我主要避免使用的一种方法。

它有点难,因为它在模板中,所以模板中的命名对象无法在代码隐藏中看到。

希望有所帮助

于 2012-05-03T11:54:55.747 回答