gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:37:25 25 4
gpt4 key购买 nike

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

然后运行应用程序,结果“文本”工作正常,但“偏移”不起作用。
而我在Windows Phone 7开发环境中尝试了类似的东西,得到了同样的结果。

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

谢谢

最佳答案

尝试:

{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}

关于wpf - TemplateBinding 在某些情况下不起作用(使用 TranslateTransform 时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6346594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com