Esta es la forma I reproducido este problema en WPF:TemplateBinding no funciona en ciertos casos (cuando se utiliza TranslateTransform)
Crear un control personalizado:
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));
}
Añadir un estilo para que en el genérico. archivo 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>
luego añadirlo a la ventana principal:
<local:TestCustomControl OffSet="32" Text="the off set is not working" FontSize="36">
</local:TestCustomControl>
Luego, ejecute la aplicación, resultó que el "Texto" funciona bien, pero el "Desactivado" no funciona. Y probé algo similar en el entorno de desarrollo de Windows Phone 7, y obtuve el mismo resultado.
¿Cómo debo modificar el código para que OffSet funcione?
Gracias
De acuerdo con "WPF 4.5 Unleashed", Nathan, Adam; 3ª ed. do. 2014, p. 437, no puede usar 'TemplateBinding' en las propiedades de' Freezeable'. 'TranslateTransform' es un' Freezeable', por eso no funcionó (mientras que 'TextBlock' no es' Freezeable', por eso funcionó allí). Lo desconcertante es que no se trata de un error en tiempo de ejecución (o en cualquier otro momento). Simplemente falla en silencio. –