Pregunta

Así es como reproducí este problema en WPF:

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));
}

Agregue un estilo en el archivo genic.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 agrégalo 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 "desplazamiento" no funciona. Y probé lo similar en el entorno de desarrollo de Windows Phone 7, y obtuve el mismo resultado.

¿Cómo debo modificar el código para que el desplazamiento funcione?

Gracias

¿Fue útil?

Solución

Probar:

{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}

Otros consejos

Tanto las plantillas como las de relativas no funcionan, así que olvídate de ello si se dirige a WP7.0 (Silverlight 3). Use algunas otras formas de hacerlo. De hecho, cambié manualmente los valores X/Y de cada transformación cada vez que cambia el "desplazamiento".

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top