TemplateBinding 在某些情况下不起作用(使用 TranslateTransform 时) [英] TemplateBinding does not work in certain cases(when using TranslateTransform)
本文介绍了TemplateBinding 在某些情况下不起作用(使用 TranslateTransform 时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我在 WPF 中重现这个问题的方式:
This is how I reproduced this problem in 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 文件中为其添加样式:
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>
然后运行应用程序,结果文本"工作正常,但偏移"不起作用.而我在 Windows Phone 7 开发环境中尝试过类似的事情,也得到了同样的结果.
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.
我应该如何修改代码才能使 OffSet 工作?
How should I modify the code to make the OffSet work?
谢谢
推荐答案
尝试:
{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}
这篇关于TemplateBinding 在某些情况下不起作用(使用 TranslateTransform 时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文