如何使用Child-deined Attached Properties?

 mobiledu2502926247 发布于 2023-02-05 12:04

我把你的例子作为基础,删除了一点,并得到了这个例子.我删除它们以显示最小的工作示例.

首先,我删除了CellBorderThickness属性,因为已经附加了依赖属性.

Son

// Removed
public Thickness CellBorderThickness
{
    get { return (Thickness)GetValue(CellBorderThicknessProperty); }
    set { SetValue(CellBorderThicknessProperty, value); }
}

在我父亲的控件中我删除了OnApplyTemplate(),并在函数中SetBorderThickness()使用附加依赖属性的机会来设置值:

Father

// Removed
OnApplyTemplate() { ... }

// Add
Son.SetCellBorderThickness(childControl, value);

以下是一个完整的例子.结构示例:

在此输入图像描述

XAML

Styles

儿子


父亲


主窗口


    

    

Code

儿子

public class Son : Control
{
    public static readonly DependencyProperty CellBorderThicknessProperty =
                                              DependencyProperty.RegisterAttached("CellBorderThickness",
                                              typeof(Thickness), typeof(Son),
                                              new FrameworkPropertyMetadata(new Thickness(2),
                                                                            FrameworkPropertyMetadataOptions.AffectsRender,
                                                                            CellBorderThicknessProperty_ChangedCallBack));

    public static void SetCellBorderThickness(UIElement obj, Thickness value)
    {
        obj.SetValue(Son.CellBorderThicknessProperty, value);
    }

    public static Thickness GetCellBorderThickness(UIElement obj)
    {
        return (Thickness)obj.GetValue(Son.CellBorderThicknessProperty);
    }

    private static void CellBorderThicknessProperty_ChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Father father = d as Father;

        if (father != null)
        {
            father.SetBorderThickness((Thickness)e.NewValue);
        }
    }
}    

父亲

public class Father : Control
{
    private Son childControl; 

    public void SetBorderThickness(Thickness value)
    {
        if (childControl != null)
        {
            Son.SetCellBorderThickness(childControl, value);
        }
    }
}

Output

在此输入图像描述

项目可在此链接中找到.

1 个回答
  • 我把你的例子作为基础,删除了一点,并得到了这个例子.我删除它们以显示最小的工作示例.

    首先,我删除了CellBorderThickness属性,因为已经附加了依赖属性.

    Son

    // Removed
    public Thickness CellBorderThickness
    {
        get { return (Thickness)GetValue(CellBorderThicknessProperty); }
        set { SetValue(CellBorderThicknessProperty, value); }
    }
    

    在我父亲的控件中我删除了OnApplyTemplate(),并在函数中SetBorderThickness()使用附加依赖属性的机会来设置值:

    Father

    // Removed
    OnApplyTemplate() { ... }
    
    // Add
    Son.SetCellBorderThickness(childControl, value);
    

    以下是一个完整的例子.结构示例:

    在此输入图像描述

    XAML

    Styles

    儿子

    <Style TargetType="{x:Type SonNamespace:Son}">
        <Setter Property="Background" Value="Gainsboro" />
        <Setter Property="BorderBrush" Value="Black" />
        <Setter Property="Width" Value="100" />
        <Setter Property="Height" Value="100" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding SonNamespace:Son.CellBorderThickness}">
    
                        <ContentPresenter Content="I'am a Son" 
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    父亲

    <Style TargetType="{x:Type FatherNamespace:Father}">
        <Setter Property="Background" Value="AliceBlue" />
        <Setter Property="BorderBrush" Value="Black" />
        <Setter Property="Width" Value="100" />
        <Setter Property="Height" Value="100" />
        <Setter Property="HorizontalAlignment" Value="Right" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding SonNamespace:Son.CellBorderThickness}">
    
                        <ContentPresenter Content="I'am a Father" 
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    主窗口

    <Grid>
        <SonNamespace:Son />
    
        <FatherNamespace:Father SonNamespace:Son.CellBorderThickness="6" />
    </Grid>
    

    Code

    儿子

    public class Son : Control
    {
        public static readonly DependencyProperty CellBorderThicknessProperty =
                                                  DependencyProperty.RegisterAttached("CellBorderThickness",
                                                  typeof(Thickness), typeof(Son),
                                                  new FrameworkPropertyMetadata(new Thickness(2),
                                                                                FrameworkPropertyMetadataOptions.AffectsRender,
                                                                                CellBorderThicknessProperty_ChangedCallBack));
    
        public static void SetCellBorderThickness(UIElement obj, Thickness value)
        {
            obj.SetValue(Son.CellBorderThicknessProperty, value);
        }
    
        public static Thickness GetCellBorderThickness(UIElement obj)
        {
            return (Thickness)obj.GetValue(Son.CellBorderThicknessProperty);
        }
    
        private static void CellBorderThicknessProperty_ChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Father father = d as Father;
    
            if (father != null)
            {
                father.SetBorderThickness((Thickness)e.NewValue);
            }
        }
    }    
    

    父亲

    public class Father : Control
    {
        private Son childControl; 
    
        public void SetBorderThickness(Thickness value)
        {
            if (childControl != null)
            {
                Son.SetCellBorderThickness(childControl, value);
            }
        }
    }
    

    Output

    在此输入图像描述

    项目可在此链接中找到.

    2023-02-05 12:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有