テンプレートバインディングは、特定の場合には機能しません(transTeTransFormを使用する場合)

StackOverflow https://stackoverflow.com/questions/6346594

質問

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

オフセットを機能させるためにコードを変更するにはどうすればよいですか?

ありがとう

役に立ちましたか?

解決

試す:

{Binding Offset, RelativeSource={RelativeSource TemplatedParent}}

他のヒント

テンプレートと親RelativeSourceの両方は機能しませんので、WP7.0(Silverlight 3)をターゲットにしている場合は、それを忘れてください。他のいくつかの方法を使用してそれを動かしてください。 「オフセット」が変更されるたびに、各変換のX/Y値を実際に手動で変更しました。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top