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

发布于 2024-11-15 04:23:12 字数 2250 浏览 4 评论 0原文

这就是我在 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, styling, binding, etc...
public static readonly DependencyProperty OffSetProperty =
    DependencyProperty.Register("OffSet", typeof(double), typeof(TestCustomControl), 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>

然后运行应用程序,结果发现“Text”工作正常,但“OffSet”不起作用。 我在Windows Phone 7开发环境中尝试了类似的事情,得到了相同的结果。

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

谢谢

This is how I reproduced this problem in WPF:

Create a custom control:

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, styling, binding, etc...
public static readonly DependencyProperty OffSetProperty =
    DependencyProperty.Register("OffSet", typeof(double), typeof(TestCustomControl), new PropertyMetadata(5.0));
}

Add a style for it in the Generic.xaml file:

<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>

Then add it to the main window:

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

    </local:TestCustomControl>

Then run the application, it turned out the "Text" works fine but the "OffSet" does not work.
And I tried the similar thing in Windows Phone 7 development environment, and I got the same result.

How should I modify the code to make the OffSet work?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-11-22 04:23:12

尝试:

{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}

Try:

{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}
安稳善良 2024-11-22 04:23:12

TemplateBing 和RelativeSource 都不起作用,所以如果您的目标是WP7.0 (Silverlight 3),请忽略它。
使用一些其他方法来解决这个问题。
实际上,每次更改“OffSet”时,我都会手动更改每个变换的 X/Y 值。

Both of TemplateBing and RelativeSource do not work, so just forget about it if you are targeting WP7.0 (Silverlight 3).
Use some other ways to work it around.
I actually manually changed the X/Y values of each transform every time the "OffSet" is changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文