【问题标题】:Template binding to ScaleTransform not work in Custom Control与 ScaleTransform 的模板绑定在自定义控件中不起作用
【发布时间】:2014-08-31 17:17:17
【问题描述】:

我创建了从 Control 派生的简单自定义控件。

这个控件有 2 个 DP,我在 ScaleTransform 上的 xaml 中绑定。

代码隐藏。

public class MyControl : Control
{
        public static readonly DependencyProperty ScaleXProperty = DependencyProperty.Register(
        "ScaleX", typeof (double), typeof (MyControl), new FrameworkPropertyMetadata(OnScaleXChanged));

    public static readonly DependencyProperty ScaleYProperty = DependencyProperty.Register(
        "ScaleY", typeof (double), typeof (MyControl), new FrameworkPropertyMetadata(OnScaleYChanged));

                public double ScaleX
    {
        get { return (double) GetValue(ScaleXProperty); }
        set { SetValue(ScaleXProperty, value); }
    }

    public double ScaleY
    {
        get { return (double) GetValue(ScaleYProperty); }
        set { SetValue(ScaleYProperty, value); }
    }
}

XAML。

    <Style TargetType="{x:Type local:MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    <Border.LayoutTransform>
                        <ScaleTransform ScaleX="{TemplateBinding ScaleX}" ScaleY="{TemplateBinding ScaleY}" />
                    </Border.LayoutTransform>


                    <Image HorizontalAlignment="Stretch"
                           VerticalAlignment="Stretch"
                           Source="{TemplateBinding Icon}"
                           StretchDirection="Both" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我在 Window 中使用 MyControl。在我在 LayoutTransform 后面的 Window 代码中更改了 ScaleX 和 ScaleY 属性后,没有火。

所以我在 MyControl 中为 ScaleX 和 ScaleY 添加了处理程序。在这些处理程序中,我手动执行 ScaleTransform。这行得通。那么 TemplateBinding 的问题在哪里呢?

使用此解决方法 ScaleTransform 有效。

    private static void OnScaleXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is MyControl)
        {
            var ctrl = d as MyControl;

            var x = (double) e.NewValue;

            ctrl.LayoutTransform = new ScaleTransform(x, ctrl.ScaleY);

        }
    }

【问题讨论】:

    标签: wpf custom-controls scaletransform layouttransform


    【解决方案1】:

    您可能遇到了TemplateBinding 的众多限制之一。

    因此,请改用带有 relative source 的等效 Binding 作为 模板化父级,这在语义上等同于 TemplateBinding,但(稍微)重一些:

    <ScaleTransform ScaleX="{Binding ScaleX,RelativeSource={RelativeSource TemplatedParent}}" ScaleY="{Binding ScaleY,RelativeSource={RelativeSource TemplatedParent}}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2015-04-25
      • 2012-12-12
      相关资源
      最近更新 更多