询问者
设计时自定义控件无法显示

问题
-
在一个Grid里面放了一个自定义的ImageButton,但是在设计时无法显示,XAML编辑器报了这样一个异常,但是运行时确实好的,网上有朋友说这是一个VS的Bug。。。
<quote>
ArgumentException: 值不在预期的范围内。
在 MS.Internal.XcpImports.CheckHResult(UInt32 hr)
在 MS.Internal.XcpImports.Collection_InsertValue[T](PresentationFrameworkCollection`1 collection, UInt32 index, CValue value)
在 MS.Internal.XcpImports.Collection_InsertDependencyObject[T](PresentationFrameworkCollection`1 collection, UInt32 index, DependencyObject value)
在 System.Windows.PresentationFrameworkCollection`1.InsertDependencyObject(Int32 index, DependencyObject value)
在 System.Windows.Controls.UIElementCollection.InsertInternal(Int32 index, UIElement value)</quote> 代码如下:<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ArtBoard.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Style TargetType="local:ImageButton"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Margin" Value="0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:ImageButton"> <Grid x:Name="RootGrid" > <Image x:Name="BtnImage" Stretch="Uniform" Source="{TemplateBinding NormalSource}" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
public class ImageButton : Control { public ImageButton() { DefaultStyleKey = typeof(ImageButton); DataContext = this; } private Grid _panleGd; private Image _image; public event EventHandler BtnClick; public ImageSource NormalSource { get { return (ImageSource)GetValue(NormalSourceProperty); } set { SetValue(NormalSourceProperty, value); } } public ImageSource PressedSource { get { return (ImageSource)GetValue(PressedSourceProperty); } set { SetValue(PressedSourceProperty, value); } } public static readonly DependencyProperty NormalSourceProperty = DependencyProperty.Register("MyProperty", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null)); public static readonly DependencyProperty PressedSourceProperty = DependencyProperty.Register("PressedSource", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null)); public override void OnApplyTemplate() { _panleGd = GetTemplateChild("RootGrid") as Grid; _image = GetTemplateChild("BtnImage") as Image; base.OnApplyTemplate(); } protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); if (PressedSource != null) _image.Source = PressedSource; } protected override void OnMouseLeave(MouseEventArgs e) { base.OnMouseLeave(e); _image.Source = NormalSource; } protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); _image.Source = NormalSource; if (BtnClick != null) BtnClick(this, EventArgs.Empty); } }
全部回复
-
您好,在这个scenario中,您不需要如此大费周章,您可以参考这份代码