繁体   English   中英

Silverlight与DependencyProperty和ControlTemplate的使用

[英]Silverlight with using of DependencyProperty and ControlTemplate

我开始研究Silverlight 3和Visual Studio2008。我一直在尝试创建带有看起来像圆形的按钮控件的Windows边栏小工具(我有几个“圆形”的png图像)。 我想要的行为如下:当鼠标悬停在图像上时,它会变大一点。 当我们单击它时,它就会上下移动。 当我们离开按钮的图像时,它将再次变为正常大小。
因为我将要拥有几个这样的控件,所以我决定实现自定义控件:就像一个按钮,但是带有图像,没有内容文本。
我的问题是我无法在模板和样式中设置自定义属性。

我究竟做错了什么?

我的teamplate控件具有三个附加属性:

namespace SilverlightGadgetDocked {  
    public class ActionButton : Button {  
        /// <summary>  
        /// Gets or sets the image source of the button.  
        /// </summary>  
        public String ImageSource {  
            get { return (String)GetValue(ImageSourceProperty); }  
            set { SetValue(ImageSourceProperty, value); }  
        }  
        /// <summary>  
        /// Gets or sets the ratio that is applied to the button's size  
        /// when the mouse control is over the control.  
        /// </summary>  
        public Double ActiveRatio {  
            get { return (Double)GetValue(ActiveRatioProperty); }  
            set { SetValue(ActiveRatioProperty, value); }  
        }

        /// <summary>
        /// Gets or sets the offset - the amount of pixels the button 
        /// is shifted when the the mouse control is over the control.
        /// </summary>
        public Double ActiveOffset {
            get { return (Double)GetValue(ActiveOffsetProperty); }
            set { SetValue(ActiveOffsetProperty, value); }
        }

        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource",
            typeof(String),
            typeof(ActionButton),
            new PropertyMetadata(String.Empty));

        public static readonly DependencyProperty ActiveRatioProperty =
            DependencyProperty.Register("ActiveRatio",
            typeof(Double),
            typeof(ActionButton),
            new PropertyMetadata(1.0));

        public static readonly DependencyProperty ActiveOffsetProperty =
            DependencyProperty.Register("ActiveOffset",
            typeof(Double),
            typeof(ActionButton),
            new PropertyMetadata(0));

        public ActionButton() {
            this.DefaultStyleKey = typeof(ActionButton);
        }
    }
}

以及具有样式的XAML:

<UserControl x:Class="SilverlightGadgetDocked.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:SilverlightGadgetDocked="clr-namespace:SilverlightGadgetDocked" 
    Width="130" Height="150" SizeChanged="UserControl_SizeChanged"  MouseEnter="UserControl_MouseEnter" MouseLeave="UserControl_MouseLeave">
    <Canvas>
        <Canvas.Resources>
            <Style x:Name="ActionButtonStyle" TargetType="SilverlightGadgetDocked:ActionButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="SilverlightGadgetDocked:ActionButton">
                            <Grid>
                                <Image Source="{TemplateBinding ImageSource}"
                                    Width="{TemplateBinding Width}"
                                    Height="{TemplateBinding Height}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="DockedActionButtonStyle" TargetType="SilverlightGadgetDocked:ActionButton"
                   BasedOn="{StaticResource ActionButtonStyle}">
                <Setter Property="Canvas.ZIndex" Value="2"/>
                <Setter Property="Canvas.Top" Value="10"/>
                <Setter Property="Width" Value="30"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="ActiveRatio" Value="1.15"/>
                <Setter Property="ActiveOffset" Value="5"/>
            </Style>
            <Style x:Key="InfoActionButtonStyle" TargetType="SilverlightGadgetDocked:ActionButton"
                   BasedOn="{StaticResource DockedActionButtonStyle}">
                   <Setter Property="ImageSource" Value="images/action_button_info.png"/>
            </Style>
            <Style x:Key="ReadActionButtonStyle" TargetType="SilverlightGadgetDocked:ActionButton"
                   BasedOn="{StaticResource DockedActionButtonStyle}">
                <Setter Property="ImageSource" Value="images/action_button_read.png"/>
            </Style>
            <Style x:Key="WriteActionButtonStyle" TargetType="SilverlightGadgetDocked:ActionButton"
                   BasedOn="{StaticResource DockedActionButtonStyle}">
                <Setter Property="ImageSource" Value="images/action_button_write.png"/>
            </Style>
        </Canvas.Resources>
        <StackPanel>
                <Image Source="images/background_docked.png" Stretch="None"/>
                <TextBlock Foreground="White" MaxWidth="130" HorizontalAlignment="Right" VerticalAlignment="Top" Padding="0,0,5,0" Text="Name" FontSize="13"/>
        </StackPanel>
        <SilverlightGadgetDocked:ActionButton Canvas.Left="15" Style="{StaticResource InfoActionButtonStyle}" MouseLeftButtonDown="imgActionInfo_MouseLeftButtonDown"/>
        <SilverlightGadgetDocked:ActionButton Canvas.Left="45" Style="{StaticResource ReadActionButtonStyle}" MouseLeftButtonDown="imgActionRead_MouseLeftButtonDown"/>
        <SilverlightGadgetDocked:ActionButton Canvas.Left="75" Style="{StaticResource WriteAtionButtonStyle}" MouseLeftButtonDown="imgActionWrite_MouseLeftButtonDown"/>
    </Canvas>
</UserControl>

并且Visual Studio在第27行报告“ 属性Property的无效属性值ActiveRatio

<Setter Property="ActiveRatio" Value="1.15"/>

非常感谢!!!

老实说,我看不到您发布的代码有什么问题。 也许是对导致您所看到的错误的确切原因的解释可能会为您提供一些可以使用的线索。

在这里重要的是Dependency属性的注册:

    public static readonly DependencyProperty ActiveRatioProperty =
        DependencyProperty.Register("ActiveRatio",
        typeof(Double),
        typeof(ActionButton),
        new PropertyMetadata(1.0));

这将创建并注册对字符串“ActiveRatio”和类型的组合的依赖属性的实例ActionButton 当Silverlight将以下Xaml付诸实践时:-

<Style x:Key="Stuff" TargetType="local:ActionButton">
  <Setter Property="ActiveRatio" Value="1.15" />
</Style>

它将样式的TargetType属性中指定的类型与setters Property的字符串结合在一起,以找到依赖项属性实例。 *然后,可以使用由dependency属性指示的类型来转换setters Value属性中的字符串。 最后,它可以在设置了样式的FrameworkElement上调用SetValue ,并传递找到的DependencyProperty和转换后的值。

现在返回上一段中的*。 在这一点上,代码已失败。 它没有找到字符串“ActiveRatio”和类型的依赖产权登记ActionButton

我无法告诉您其失败的原因,您的代码清楚地注册了该名称,并且样式中的类型与注册中传递的类型匹配。 我什至为您的代码编写了一个小版本,并且运行良好。

我只能建议您尝试完整的Rebuild,然后运行代码。

假设您发布的内容相当完整,那么我仅有的另一条建议就是这样的“抓稻草”练习,我什至不解释我的原因。 尝试将其添加到您的ActionButton类中:

 public static ActionButton() { }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM