首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ControlTemplate LayoutTransform绑定System.Windows.Data错误2或4

ControlTemplate LayoutTransform绑定System.Windows.Data错误2或4
EN

Stack Overflow用户
提问于 2018-03-01 10:33:37
回答 1查看 248关注 0票数 1

我正在创建一个自定义的UserControl,它将充当一个容器,在其内容后面显示一个大小大小的水印。

ControlTemplate的相关部分(下面我将给您完整的说明)是

代码语言:javascript
复制
    <TextBlock
                        Text="{TemplateBinding WatermarkText}"
                        Foreground="{TemplateBinding WatermarkBrush}"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        FontWeight="Bold">
                                <TextBlock.LayoutTransform>
                                    <RotateTransform Angle="{Binding WatermarkAngle,RelativeSource={RelativeSource AncestorType={x:Type local:WatermarkUserControl}}}"/>
                                </TextBlock.LayoutTransform>
                            </TextBlock>

我的UserControl具有WatermarkText、WatermarkBrush、WatermarkAngle和WatermarkVisibility的依赖属性(我将在下面包含这些属性)。注意,TemplateBindings for WatermarkText、WatermarkBrush和WatermarkVisibility都很好。

将TemplateBinding用于WatermarkAngle不起作用,因为TemplateBinding是一个轻量级的“绑定”,因此它不支持继承语境。我最终得到的WatermarkAngle绑定(上面)确实有效;但是报告了一个绑定错误:

System.Windows.Data错误:4:无法找到与引用'RelativeSource FindAncestor,AncestorType='redacted Nampace.WatermarkUserControl‘,AncestorLevel=’1‘绑定的源代码。BindingExpression:Path=WatermarkAngle;DataItem=null;目标元素为“RotateTransform”(HashCode=59772470);目标属性为“角”(类型为“Double”)

那么,是否有更好的方法来避免报告错误呢?那么,考虑到绑定实际上是有效的,它为什么报告绑定错误呢?(如果我改变了值,它就反映了这一点。)

问题到此结束。以下是您需要的满足MCVE的所有部件

代码语言:javascript
复制
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

public class WatermarkUserControl : UserControl
{
    public static readonly DependencyProperty WatermarkTextProperty =
        DependencyProperty.Register(nameof(WatermarkText), typeof(string), typeof(WatermarkUserControl), new PropertyMetadata("Watermark"));
    public static readonly DependencyProperty WatermarkBrushProperty =
        DependencyProperty.Register(nameof(WatermarkBrush), typeof(Brush), typeof(WatermarkUserControl), new PropertyMetadata(new SolidColorBrush(Colors.LightGray)));
    public static readonly DependencyProperty WatermarkAngleProperty =
        DependencyProperty.Register(nameof(WatermarkAngle), typeof(double), typeof(WatermarkUserControl), new PropertyMetadata(0d));
    public static readonly DependencyProperty WatermarkVisibilityProperty =
        DependencyProperty.Register(nameof(WatermarkVisibility), typeof(Visibility), typeof(WatermarkUserControl), new PropertyMetadata(Visibility.Visible));

    public string WatermarkText
    {
        get { return (string)GetValue(WatermarkTextProperty); }
        set { SetValue(WatermarkTextProperty, value); }
    }

    public Brush WatermarkBrush
    {
        get { return (Brush)GetValue(WatermarkBrushProperty); }
        set { SetValue(WatermarkBrushProperty, value); }
    }

    public double WatermarkAngle
    {
        get { return (double)GetValue(WatermarkAngleProperty); }
        set { SetValue(WatermarkAngleProperty, value); }
    }

    public Visibility WatermarkVisibility
    {
        get { return (Visibility)GetValue(WatermarkVisibilityProperty); }
        set { SetValue(WatermarkVisibilityProperty, value); }
    }
}

ResourceDictionary:

代码语言:javascript
复制
<Style x:Key="WatermarkUserControlBaseStyle" TargetType="local:WatermarkUserControl">
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:WatermarkUserControl">
                <Grid>
                    <local:NoSizeDecorator Visibility="{TemplateBinding WatermarkVisibility}">
                        <Viewbox>
                            <TextBlock
                                Text="{TemplateBinding WatermarkText}"
                                Foreground="{TemplateBinding WatermarkBrush}"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                FontWeight="Bold">
                                <TextBlock.LayoutTransform>
                                    <RotateTransform Angle="{Binding WatermarkAngle,RelativeSource={RelativeSource AncestorType={x:Type local:WatermarkUserControl}}}"/>
                                </TextBlock.LayoutTransform>
                            </TextBlock>
                        </Viewbox>
                    </local:NoSizeDecorator>
                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="DraftWatermarkStyle" TargetType="local:WatermarkUserControl" BasedOn="{StaticResource WatermarkUserControlBaseStyle}">
    <Setter Property="WatermarkText" Value="DRAFT"/>
    <Setter Property="WatermarkBrush" Value="LightPink"/>
    <Setter Property="WatermarkAngle" Value="-20"/>
</Style>

NoSizeDecorator被定义为这里

使用实例:

代码语言:javascript
复制
<local:WatermarkUserControl
    Style="{StaticResource DraftWatermarkStyle}"
    WatermarkVisibility="True">
    <StackPanel>
        <Label Content="Mr Smith"/>
        <Label Content="1 High Street"/>
        <Label Content="London"/>
    </StackPanel>
</local:WatermarkUserControl>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-20 11:03:17

我想出了个办法。它使错误消失了,而且它似乎仍然有效,而且我还没有发现任何副作用。

我将RotateTransform声明为ControlTemplate视图框中的本地资源,然后将其绑定为StaticResource到TextBlock的LayoutTransform属性。因此问题中的Viewbox的新版本如下所示:

代码语言:javascript
复制
<Viewbox>
    <Viewbox.Resources>
        <RotateTransform x:Key="RotateWatermarkTransform" Angle="{Binding WatermarkAngle,RelativeSource={RelativeSource TemplatedParent}}"/>
    </Viewbox.Resources>

    <TextBlock
        Text="{TemplateBinding WatermarkText}"
        Foreground="{TemplateBinding WatermarkBrush}"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        FontWeight="Bold"
        LayoutTransform="{StaticResource RotateWatermarkTransform}"/>
</Viewbox>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49047739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档