none
如何使控件模板对中有关Margin,padding,BorderThickness这些属性的部分数值TemplateBinding设置? RRS feed

  • 问题

  • 为了便于说明,用如下控件模板

        <ControlTemplate x:Key="sss">
            <Border BorderThickness="{TemplateBinding BorderThickness}">
                <Grid>
                    <Border Margin="5" BorderThickness="0,0,0,{TemplateBinding BorderThickness}">
                        <TextBlock></TextBlock>
                    </Border>
                </Grid>
            </Border>
        </ControlTemplate>

    这个空间模板的代码的顶级是个Border,内部套一个Grid,然后再套一个Border,最后是TextBlock,这样搞是想通过对使用该模板的控件的BorderThickness的xaml上设置。对控件的最外边的线和内部的TextBlock下边的边框的粗细线做到一起设置

    当然,这个代码是编译不能通过的,因为BorderThickness在xaml上只支持整体的TemplateBinding设置,不支持对部分数值设置。如何做到对部分数值的{TemplateBinding BorderThickness},同样如果margin,padding可能会要这样的设置,要如何做到?



    2019年1月9日 8:27

全部回复

  • 虽然不知道你为什么要这么做

    但是可以通过附加属性/依赖属性来实现你的目的

    请参考

    依赖属性

    附加属性

    依赖属性和附加属性的做法相似 我给出附加属性的margin的办法 其他类似

    附加类

    public  class ATCH
        {
            public static readonly DependencyProperty MarginTopProperty = DependencyProperty.RegisterAttached(" MarginTop", typeof(double), typeof(ATCH), new PropertyMetadata(0.0, new PropertyChangedCallback(CallBackForMaginTop)));
    
            public static void SetMarginTop(DependencyObject dependencyObject, double value) => dependencyObject.SetValue(MarginTopProperty, value);
    
            public static double GetMarginTop(DependencyObject dependencyObject) => (double)dependencyObject.GetValue(MarginTopProperty);
    
            private static void CallBackForMaginTop(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if (d == null)
                    throw new NotImplementedException();
    
                var element = d as FrameworkElement; //UIElement;
    
                if (!(e.NewValue is double))
                    return;//throw new NotFiniteNumberException();
    
                element.Margin = new Thickness(element.Margin.Left, (double)e.NewValue, element.Margin.Right, element.Margin.Bottom);
            }
    
            /******************************************************************************
            *上方为Margin的Top参数
            * 
            * 
            * 
            * 
            * 下方为Margin的Left参数
            *****************************************************************************/
    
            public static readonly DependencyProperty MarginLeftProperty = DependencyProperty.RegisterAttached("MarginLeft",typeof(double),typeof(ATCH),new PropertyMetadata(0.0,new PropertyChangedCallback(CallBackForMaginLeft)));
    
            public static void SetMarginLeft(DependencyObject dependencyObject, double value) => dependencyObject.SetValue(MarginLeftProperty, value);
    
            public static double GetMarginLeft(DependencyObject dependencyObject) => (double)dependencyObject.GetValue(MarginLeftProperty);
    
            private static void CallBackForMaginLeft(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if(d==null)
                throw new NotImplementedException();
    
                var element = d as FrameworkElement; //UIElement;
    
                if (!(e.NewValue is double))
                    return;//throw new NotFiniteNumberException();
    
                element.Margin = new Thickness((double)e.NewValue, element.Margin.Top, element.Margin.Right, element.Margin.Bottom);
                
            }
        }

    XAML

    <Grid>
            <Button Height="50" Width="50" local:ATCH.MarginLeft="30" local:ATCH.MarginTop="20"/>
        </Grid>

    2019年1月9日 14:56