7

これが、この問題を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開発環境でも同様のことを試しましたが、同じ結果が得られました。

オフセットを機能させるには、コードをどのように変更する必要がありますか?

ありがとう

4

2 に答える 2

21

試す:

{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}
于 2011-06-14T16:10:17.560 に答える
1

TemplateBingとRelativeSourceはどちらも機能しないため、WP7.0(Silverlight 3)をターゲットにしている場合は忘れてください。それを回避するために他のいくつかの方法を使用してください。「オフセット」を変更するたびに、実際に各変換のX/Y値を手動で変更しました。

于 2011-06-17T04:36:08.347 に答える