WPF属性(二)附加属性

附加属性是说一个属性本来不属于某个对象,但由于某种需求而被后来附加上,也就是把对象放入一个特定环境后对象才具有的属性就称为附加属性,附加属性的作用就是将属性与数据类型解耦,让数据类型的设计更加灵活,举例,一个TextBox被放在不同的布局容器中时就会有不同的布局属性,这些属性就是由布局容器为TextBox附加上的,附加属性的本质就是依赖属性,二者仅仅在注册和包装器上有一点区别

小技巧,在VS中输入propa后,连按两次tab键,可以添加好一个附加属性的框架,继续按tab键,可以继续修改附加属性的内容


举个例子,Person这个类,放在学校中就会获得年级这个属性,那么准备一个School类,School类继承DependencyObject,定义一个附加属性

    public class School : DependencyObject
    {
        public static int GetGrade(DependencyObject obj)
        {
            return (int)obj.GetValue(GradeProperty);
        }

        public static void SetGrade(DependencyObject obj, int value)
        {
            obj.SetValue(GradeProperty, value);
        }

        public static readonly DependencyProperty GradeProperty =
            DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School), new UIPropertyMetadata(0));
    }

可以看到,附加属性已依赖属性有两点不同:

一。附加属性使用的RegisterAttached方法,而依赖属性使用的是Register方法

二。附加属性使用两个方法进行包装,依赖属性使用CLR属性对GetValue和SetValue两个方法进行包装

如何使用School的GradeProperty呢,首先准备一个DependencyObject的派生类Human

    public class Human : DependencyObject
    {

    }

使用这个附加属性

            Human human = new Human();
            School.SetGrade(human, 6);
            MessageBox.Show(School.GetGrade(human).ToString());
看看实例代码,如果要在一个3行3列的表格布局的最中间一格放置一个按钮,界面代码如下:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Button Grid.Row="1" Grid.Column="1" Content="OK" />
    </Grid>
</Window>

运行后效果如图:


那么,如果用C#代码来实现这个效果,代码如下:

            Grid grid = new Grid() { ShowGridLines = true };
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());

            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());

            Button button = new Button() { Content = "OK" };
            Grid.SetRow(button, 1);
            Grid.SetColumn(button, 1);

            grid.Children.Add(button);
            Content = grid;

效果与上图一致,附加属性的用法很奇怪吧,附加属性同时也是依赖属性,再附上一个例子,用两个滑块分别控制按钮在九宫格中的位置

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="420" Width="525">
    <StackPanel>
        <Grid ShowGridLines="True" Height="300">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Button Content="OK" Grid.Row="{Binding ElementName=slider1, Path=Value}" Grid.Column="{Binding ElementName=slider2, Path=Value}" />
        </Grid>
        <Slider x:Name="slider1" Minimum="0" Maximum="2" />
        <Slider x:Name="slider2" Minimum="0" Maximum="2" />
    </StackPanel>
</Window>


  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值