【问题标题】:TemplateBinding does not work in certain cases(when using TranslateTransform)TemplateBinding 在某些情况下不起作用(使用 TranslateTransform 时)
【发布时间】:2011-06-17 12:37:04
【问题描述】:

这就是我在 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 工作?

谢谢

【问题讨论】:

  • 根据“WPF 4.5 Unleashed”,内森,亚当;第三版。 C。 2014 年,第437,您不能在Freezeable 的属性上使用TemplateBindingTranslateTransformFreezeable,所以这就是它不起作用的原因(而 TextBlock 不是 Freezeable,所以这就是它在那里起作用的原因)。令人费解的是,这不是运行时(或任何其他时间)错误。它只是默默地失败了。

标签: wpf silverlight windows-phone-7 templatebinding


【解决方案1】:

试试:

{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}

【讨论】:

  • 刚刚又测试了,Silverlight3不行,Silverlight4不行。
  • WP7.0 有一个相当稀的“Silverlight 4”。 WP7.1(又名 Mango)附带的 Silverlight4 实现更加完整,CuiPengFei 的解决方案可能会开始工作。
  • 我现在正在运行 Mango,而 Kent 的解决方案(完整的相关源语法)确实可以在 Mango 下运行。这也很好,因为现在 TemplateBinding 语法在框架内产生了一个“未指定的异常”。如果可以的话,我建议还包括Mode=OneWay,因为这使得它在语义上与 TemplateBinding 相同,更不用说更高效了。
  • 我遇到了 TemplateBinding 的问题。将其更改为长格式帮助我找出了问题所在。谢谢!
  • 我希望我能理解那种巫术。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 2018-11-28
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2017-06-29
    相关资源
    最近更新 更多