首页 > 其他 > 详细

wpf 属性值的继承

时间:2014-01-16 20:52:22      阅读:493      评论:0      收藏:0      [点我收藏+]

//自定义canvas 布局

bubuko.com,布布扣
    //自定义canvas 布局
    class CustomCanvas : Canvas
    {        

        public static readonly DependencyProperty Text2InheritProperty =
                DependencyProperty.RegisterAttached("Text2Inherit", // 注册附加属性
                                typeof(string),
                                typeof(CustomCanvas),
                                new FrameworkPropertyMetadata("DefaultTextOfCanvas", FrameworkPropertyMetadataOptions.Inherits));  //设置依赖属性的默认值

        public string Text2Inherit
        {
            set { SetValue(Text2InheritProperty, value); }
            get { return (string)GetValue(Text2InheritProperty); }
        }
    }
bubuko.com,布布扣

    // 自定义Button控件

bubuko.com,布布扣
    class CustomButton : Button
    {
        public static readonly DependencyProperty Text2InheritProperty; //依赖属性 
        
        public string Text2Inherit
        {
            set { SetValue(Text2InheritProperty, value); }                  //新设置属性值保存在本地的effectiveValue集合中存放

            get { return (string)GetValue(Text2InheritProperty); }
        }

        static CustomButton()
        {
            //设置的依赖属性值的继承       
            Text2InheritProperty = CustomCanvas.Text2InheritProperty.AddOwner(//添加多个依赖属性的共享者
                typeof(CustomButton), new FrameworkPropertyMetadata("DefaultTextOfButton", FrameworkPropertyMetadataOptions.Inherits));

        }
    }
bubuko.com,布布扣

主界面上的使用

bubuko.com,布布扣
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions> 
        
        <local:CustomCanvas x:Name="canvas0" Width="500" Grid.Row="0">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Default Text2Inherit of CustomCanvas is: "/>
                <TextBlock x:Name="textBlock1"/>
            </StackPanel>
        </local:CustomCanvas>
        
        <local:CustomCanvas x:Name="canvas1" Width="500" Grid.Row="1">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Default Text2Inherit of CustomButton is: "/>
                <local:CustomButton x:Name="button1"/>
            </StackPanel>
        </local:CustomCanvas>
        
        <local:CustomCanvas x:Name="canvas2" Width="500" Grid.Row="2" Text2Inherit="TEXT from Canvas!!!">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="After inheriting,Text2Inherit is: "/>
                <local:CustomButton x:Name="button2"/>
            </StackPanel>
        </local:CustomCanvas>
        
    </Grid>
bubuko.com,布布扣

 运行结果

bubuko.com,布布扣

wpf 属性值的继承

原文:http://www.cnblogs.com/jiehu/p/3518799.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!