微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wpf – 在某些情况下(使用TranslateTransform时)TemplateBinding不起作用

这就是我在 WPF中重现这个问题的方法

创建自定义控件:

public class TestCustomControl : Control
{
static TestCustomControl()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl),new FrameworkPropertyMetadata(typeof(TestCustomControl)));
}

public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty,value); }
}

// Using a DependencyProperty as the backing store for Text.  This enables animation,styling,binding,etc...
public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text",typeof(string),typeof(TestCustomControl),new PropertyMetadata("Hello"));

public double OffSet
{
    get { return (double)GetValue(OffSetProperty); }
    set { SetValue(OffSetProperty,value); }
}

// Using a DependencyProperty as the backing store for OffSet.  This enables animation,etc...
public static readonly DependencyProperty OffSetProperty =
    DependencyProperty.Register("OffSet",typeof(double),new PropertyMetadata(5.0));
}

在Generic.xaml文件中为它添加样式:

<Style targettype="local:TestCustomControl">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate targettype="local:TestCustomControl">
            <Grid>
                <TextBlock Text="{TemplateBinding Text}"></TextBlock>
                <TextBlock Text="{TemplateBinding Text}">
                    <TextBlock.RenderTransform>
                        <TranslateTransform X="{TemplateBinding OffSet}" Y="{TemplateBinding OffSet}"/>
                        <!--<TranslateTransform X="10" Y="10"/>-->
                    </TextBlock.RenderTransform>
                </TextBlock>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

然后将其添加到主窗口:

<local:TestCustomControl OffSet="32" Text="the off set is not working" FontSize="36">

    </local:TestCustomControl>

然后运行应用程序,事实证明“文本”工作正常,但“OffSet”不起作用.
我在Windows Phone 7开发环境中尝试了类似的东西,我得到了相同的结果.

我应该如何修改代码以使OffSet工作?

谢谢

解决方法

尝试:

{Binding Offset,RelativeSource={RelativeSource TemplatedParent}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐