2 shenjr12345 shenjr12345 于 2014.05.06 12:39 提问

Silverlight中依赖属性设置出错

环境:Visual Studio 2010 Silverlight应用程序 实现类似微软人立方的人物关系图效果
但是出现如下错误:
未在类型“Button”中找到属性“CircleButtonWidth1” 。

求大神给看一下,这个怎么改才可以?(附MainPage.xaml和MainPage.xaml.cs如下:)
首先是MailPage.xaml:
** xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication4"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.Resources>
    <Style x:Key="CircleButton" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Height="auto" HorizontalAlignment="Center" VerticalAlignment="Center" 
                        Width="auto" IsHitTestVisible="True" Background="{x:Null}">
                        <Ellipse Margin="0,0,0,0" Stroke="{x:Null}" HorizontalAlignment="Center" 
                            VerticalAlignment="Center" Width="{TemplateBinding CircleButtonWidth1}" 
                            Height="{TemplateBinding CircleButtonHeight1}" IsHitTestVisible="False" 
                            x:Name="ellipse1" Fill="{TemplateBinding Ellipse1Fill}">
                        </Ellipse>
                        <Grid Margin="0,0,0,0" HorizontalAlignment="Center" Width="1024" Height="1024" 
                           VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" x:Name="grid" Opacity="1" 
                           Background="{x:Null}" IsHitTestVisible="True">
                            <Ellipse Fill="{TemplateBinding Ellipse2Fill}" Stroke="{TemplateBinding Ellipse2Stroke}" 
                                Margin="0,0,0,0" Width="{TemplateBinding CircleButtonWidth2}" Height="{TemplateBinding CircleButtonHeight2}" 
                                x:Name="ellipse2" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Center" VerticalAlignment="Center">
                            </Ellipse>
                            <Ellipse Margin="2,2,2,2" Fill="{TemplateBinding Ellipse3Fill}" Width="{TemplateBinding CircleButtonWidth3}"
                                Height="{TemplateBinding CircleButtonHeight3}" x:Name="ellipse3" RenderTransformOrigin="0.5,0.5" 
                                HorizontalAlignment="Center" VerticalAlignment="Center">
                            </Ellipse>
                            <TextBlock Margin="0,0,0,0" FontFamily="Microsoft YaHei" FontSize="{TemplateBinding FontSize}" 
                                Foreground="#FFFFFFFF" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="textBlock" 
                                Text="{TemplateBinding Content}" RenderTransformOrigin="0.5,0.5" IsHitTestVisible="False">
                            </TextBlock>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
    <Button Style="{StaticResource CircleButton}"  Name="myButton" Content="Click me!!!" 
            Click="myButton_Click_1" >
    </Button>
</Grid>

**

下面是MainPage.xaml.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace SilverlightApplication4
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

    private void myButton_Click(object sender, RoutedEventArgs e)
    {

    }





    public partial class CircleButton : UserControl
    {

        static CircleButton()
        {

        Ellipse1FillProperty = DependencyProperty.Register("myEllipse1Fill",
                                       typeof(Brush),
                                       typeof(CircleButton),
                                       new PropertyMetadata("默认属性值", new PropertyChangedCallback(CircleButton.OnEllipse1FillPropertyChanged)));//Ellipse1FillProperty.OnEllipse1FillPropertyChanged
        Ellipse2FillProperty = DependencyProperty.Register("myEllipse2Fill",
                                   typeof(Brush),
                                   typeof(CircleButton),
                                   new PropertyMetadata("默认属性值", new PropertyChangedCallback(CircleButton.OnEllipse2FillPropertyChanged)));
        Ellipse3FillProperty = DependencyProperty.Register("myEllipse3Fill",
                                   typeof(Brush),
                                   typeof(CircleButton),
                                   new PropertyMetadata("默认属性值", new PropertyChangedCallback(CircleButton.OnEllipse3FillPropertyChanged)));
        CircleButtonWidth1Property = DependencyProperty.Register("myCircleButtonWidth1",
                              typeof(Double),
                              typeof(CircleButton),
                              new PropertyMetadata("默认属性值", new PropertyChangedCallback(CircleButton.OnCircleButtonWidth1PropertyChanged)));
        CircleButtonWidth2Property = DependencyProperty.Register("myCircleButtonWidth2",
                          typeof(Double),
                          typeof(CircleButton),
                          new PropertyMetadata("默认属性值", new PropertyChangedCallback(CircleButton.OnCircleButtonWidth2PropertyChanged)));
        CircleButtonWidth3Property = DependencyProperty.Register("myCircleButtonWidth3",
                          typeof(Double),
                          typeof(CircleButton),
                          new PropertyMetadata("默认属性值", new PropertyChangedCallback(CircleButton.OnCircleButtonWidth3PropertyChanged)));
        }

        private static void OnEllipse1FillPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircleButton).OnEllipse1FillPropertyChanged(e);
        }
        private static void OnEllipse2FillPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircleButton).OnEllipse2FillPropertyChanged(e);
        }
        private static void OnEllipse3FillPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircleButton).OnEllipse3FillPropertyChanged(e);
        }
        private static void OnCircleButtonWidth1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircleButton).OnCircleButtonWidth1PropertyChanged(e);
        }
        private static void OnCircleButtonWidth2PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircleButton).OnCircleButtonWidth2PropertyChanged(e);
        }
        private static void OnCircleButtonWidth3PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircleButton).OnCircleButtonWidth3PropertyChanged(e);
        }
        public event DependencyPropertyChangedEventHandler Ellipse1FillPropertyChanged;
        public event DependencyPropertyChangedEventHandler Ellipse2FillPropertyChanged;
        public event DependencyPropertyChangedEventHandler Ellipse3FillPropertyChanged;
        public event DependencyPropertyChangedEventHandler CircleButtonWidth1PropertyChanged;
        public event DependencyPropertyChangedEventHandler CircleButtonWidth2PropertyChanged;
        public event DependencyPropertyChangedEventHandler CircleButtonWidth3PropertyChanged;
        void OnEllipse1FillPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
        try
            {
           // Ellipse BgTextBlock = this.GetTemplateChild("ButtonCaption") as Ellipse;
           //BgTextBlock.Fill = e.NewValue as Brush;
           if (Ellipse1FillPropertyChanged != null)

           { Ellipse1FillPropertyChanged(null, e); }
            }
        catch (Exception ex)
            {
            string ks = ex.ToString();
            }

        }
        void OnEllipse2FillPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                // Ellipse BgTextBlock = this.GetTemplateChild("ButtonCaption") as Ellipse;
                //BgTextBlock.Fill = e.NewValue as Brush;
                if (Ellipse2FillPropertyChanged != null)

                { Ellipse2FillPropertyChanged(null, e); }
            }
            catch (Exception ex)
            {
                string ks = ex.ToString();
            }

        }
        void OnEllipse3FillPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                // Ellipse BgTextBlock = this.GetTemplateChild("ButtonCaption") as Ellipse;
                //BgTextBlock.Fill = e.NewValue as Brush;
                if (Ellipse3FillPropertyChanged != null)

                { Ellipse3FillPropertyChanged(null, e); }
            }
            catch (Exception ex)
            {
                string ks = ex.ToString();
            }

        }
        void OnCircleButtonWidth1PropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                // Ellipse BgTextBlock = this.GetTemplateChild("ButtonCaption") as Ellipse;
                //BgTextBlock.Fill = e.NewValue as Brush;
                if (CircleButtonWidth1PropertyChanged != null)

                { CircleButtonWidth1PropertyChanged(null, e); }
            }
            catch (Exception ex)
            {
                string ks = ex.ToString();
            }

        }
        void OnCircleButtonWidth2PropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                // Ellipse BgTextBlock = this.GetTemplateChild("ButtonCaption") as Ellipse;
                //BgTextBlock.Fill = e.NewValue as Brush;
                if (CircleButtonWidth2PropertyChanged != null)

                { CircleButtonWidth2PropertyChanged(null, e); }
            }
            catch (Exception ex)
            {
                string ks = ex.ToString();
            }

        }
        void OnCircleButtonWidth3PropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            try
            {
                // Ellipse BgTextBlock = this.GetTemplateChild("ButtonCaption") as Ellipse;
                //BgTextBlock.Fill = e.NewValue as Brush;
                if (CircleButtonWidth3PropertyChanged != null)

                { CircleButtonWidth3PropertyChanged(null, e); }
            }
            catch (Exception ex)
            {
                string ks = ex.ToString();
            }

        }
        //有些时候,我们希望在模板生效的时候就对某些模板成员进行操作,如绑定事件,
        //调整属性等,就需要一个事件OnApplyTemplate,我们只能通过override父类的OnApplyTemplate来响应模板生效
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();


            try
            {
                var tb = GetTemplateChild("ButtonCaption") as Ellipse;
                //(GetTemplateChild("ButtonCaption") as Ellipse).Fill= this.Ellipse1Fill;
                tb.Fill= this.Ellipse1Fill;

                var tb2 = GetTemplateChild("ButtonCaption") as Ellipse;
                (GetTemplateChild("ButtonCaption") as Ellipse).Fill = this.Ellipse2Fill;
                var tb3 = GetTemplateChild("ButtonCaption") as Ellipse;
                (GetTemplateChild("ButtonCaption") as Ellipse).Fill = this.Ellipse3Fill;

                var we1 = GetTemplateChild("ButtonCaption") as Ellipse;
                we1.Width = this.CircleButtonWidth1;

                var we2 = GetTemplateChild("ButtonCaption") as Ellipse;
                we1.Width = this.CircleButtonWidth2;

                var we3 = GetTemplateChild("ButtonCaption") as Ellipse;
                we1.Width = this.CircleButtonWidth3;

                var w1 = GetTemplateChild("ButtonCaption") as Ellipse;
                we1.Width = this.CircleButtonHeight1;

                var w2 = GetTemplateChild("ButtonCaption") as Ellipse;
                we1.Width = this.CircleButtonHeight2;

                var w3 = GetTemplateChild("ButtonCaption") as Ellipse;
                we1.Width = this.CircleButtonHeight3;




            }
            catch (Exception ex)
            {
                string ke = ex.ToString();
            }

        }


        public Brush Ellipse1Fill
        {
            get
            {
                return (Brush)base.GetValue(Ellipse1FillProperty);
            }
            set
            {
                base.SetValue(Ellipse1FillProperty, value);
            }
        }
        public Brush Ellipse2Fill
        {
            get
            {
                return (Brush)base.GetValue(Ellipse2FillProperty);
            }
            set
            {
                base.SetValue(Ellipse2FillProperty, value);
            }
        }
        public Brush Ellipse3Fill
        {
            get
            {
                return (Brush)base.GetValue(Ellipse3FillProperty);
            }
            set
            {
                base.SetValue(Ellipse3FillProperty, value);
            }
        }
        public double CircleButtonWidth1
        {
            get
            {
                return (double)base.GetValue(CircleButtonWidth1Property);
            }
            set
            {
                base.SetValue(CircleButtonWidth1Property, value);
            }
        }
        public double CircleButtonWidth2
        {
            get
            {
                return (double)base.GetValue(CircleButtonWidth2Property);
            }
            set
            {
                base.SetValue(CircleButtonWidth2Property, value);
            }
        }
        public double CircleButtonWidth3
        {
            get
            {
                return (double)base.GetValue(CircleButtonWidth3Property);
            }
            set
            {
                base.SetValue(CircleButtonWidth3Property, value);
            }
        }
        public double CircleButtonHeight1
        {
            get
            {
                return (double)base.GetValue(CircleButtonHeight1Property);
            }
            set
            {
                base.SetValue(CircleButtonHeight1Property, value);
            }
        }
        public double CircleButtonHeight2
        {
            get
            {
                return (double)base.GetValue(CircleButtonHeight2Property);
            }
            set
            {
                base.SetValue(CircleButtonHeight2Property, value);
            }
        }
        public double CircleButtonHeight3
        {
            get
            {
                return (double)base.GetValue(CircleButtonHeight3Property);
            }
            set
            {
                base.SetValue(CircleButtonHeight3Property, value);
            }
        }


        ///<summary>
        ///
        /// Register property.
        ///</summary>
        public static readonly DependencyProperty Ellipse1FillProperty = DependencyProperty.Register("Ellipse1Fill", typeof(Brush), typeof(CircleButton), null);
        public static readonly DependencyProperty Ellipse2FillProperty = DependencyProperty.Register("Ellipse2Fill", typeof(Brush), typeof(CircleButton), null);
        public static readonly DependencyProperty Ellipse3FillProperty = DependencyProperty.Register("Ellipse3Fill", typeof(Brush), typeof(CircleButton), null);
        public static readonly DependencyProperty CircleButtonWidth1Property = DependencyProperty.Register("CircleButtonWidth1", typeof(double), typeof(CircleButton), null);
        public static readonly DependencyProperty CircleButtonWidth2Property = DependencyProperty.Register("CircleButtonWidth2", typeof(double), typeof(CircleButton), null);
        public static readonly DependencyProperty CircleButtonWidth3Property = DependencyProperty.Register("CircleButtonWidth3", typeof(double), typeof(CircleButton), null);
        public static readonly DependencyProperty CircleButtonHeight1Property = DependencyProperty.Register("CircleButtonHeight1", typeof(double), typeof(CircleButton), null);
        public static readonly DependencyProperty CircleButtonHeight2Property = DependencyProperty.Register("CircleButtonHeight2", typeof(double), typeof(CircleButton), null);
        public static readonly DependencyProperty CircleButtonHeight3Property = DependencyProperty.Register("CircleButtonHeight3", typeof(double), typeof(CircleButton), null);




    }

    private void myButton_Click_1(object sender, RoutedEventArgs e)
    {

    }


}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!