Silverlight 4のネストされたテンプレートでのテンプレートバインディング

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

質問

コントロール、CommandTextBoxを実装しました。これは、そのすぐ横にあるボタンを備えたテキストボックスになりたいと考えています(テキストボックス内にほとんど表示されます)。

ボタンは、アイコンにバインドできる画像である必要があります。それはかなりまっすぐなものです...

public class CommandTextBox : TextBox
{
    /// <summary>
    /// The image property.
    /// </summary>
    public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
        "Image", typeof(ImageSource), typeof(CommandTextBox), null);

    /// <summary>
    ///     Initializes a new instance of the <see cref = "CommandTextBox" /> class.
    /// </summary>
    public CommandTextBox()
    {
        this.DefaultStyleKey = typeof(CommandTextBox);
    }

    /// <summary>
    ///     Gets or sets the image.
    /// </summary>
    /// <value>
    ///     The image.
    /// </value>
    public ImageSource Image
    {
        get
        {
            return (ImageSource)this.GetValue(ImageProperty);
        }

        set
        {
            this.SetValue(ImageProperty, value);
        }
    }
}

次のようにテンプレートがあります...

<Style TargetType="Controls:CommandTextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Controls:CommandTextBox">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <TextBox Grid.Column="0" Text="{TemplateBinding Text}"/>

                        <Button Grid.Column="1" 
                                Content="Search" >
                            <Button.Template>
                                <ControlTemplate>
                                    <Image Source="{TemplateBinding Image}" />
                                </ControlTemplate>
                            </Button.Template>
                        </Button>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

しかし、画像のテンプレートバインディングのためにエラーが発生します。理由を理解しています。テンプレートが今変更されたため、バインディングコンテキストは同じではありませんが、それを克服する方法がわかりません。

通常のテンプレートバインディングを行うことができるように、別の画像バットンコントロールを作成する必要がありますか、それとも別の方法がありますか?

ありがとうベン

役に立ちましたか?

解決

次のようにスタイルを変更することで、これを機能させることができました。

    <Style TargetType="appControls:CommandTextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="appControls:CommandTextBox">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Column="0" Text="{TemplateBinding Text}"/>
                        <Button Grid.Column="1" >
                            <Button.Content>                                
<Image DataContext="{TemplateBinding Image}" Source="{Binding}" />                               
                            </Button.Content>
                        </Button>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ボタンに別のテンプレートを使用していません。コントロールのある私のXAMLは次のとおりです。

<controls:CommandTextBox Text="Text" Image="/MyApp.Silverlight;component/Assets/Images/Amber_Triangle.png"></controls:CommandTextBox>

これは、あなたが望んでいた結果を達成しているようです。コントロールは、予想通りのテストページにレンダリングされます。

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