【问题标题】:TemplateBinding in a nested template in Silverlight 4Silverlight 4 中嵌套模板中的 TemplateBinding
【发布时间】:2011-03-21 11:38:35
【问题描述】:

我已经实现了一个控件,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>

但由于图像中的模板绑定,我收到错误消息。我有点明白为什么,这是因为模板现在已经改变了,所以绑定上下文不一样,但我不知道如何克服它。

我是否需要创建一个单独的 ImageButton 控件,以便我可以进行正常的模板绑定,还是有其他方法?

谢谢 本

【问题讨论】:

    标签: silverlight silverlight-4.0 templatebinding


    【解决方案1】:

    我已经设法通过如下更改样式来实现它:

        <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>
    

    这似乎达到了您所追求的结果。控件按预期呈现在我的测试页上。

    【讨论】:

    • 这不是数据绑定问题,我还没有 ViewModel。这是一个控件,模板绑定将模板中的值设置为控件属性中的值。
    • 好的,我不确定您为什么要在自己的风格中使用单独的 Button.Template。我有一个测试控件,只需在我的风格中使用 Button.Content,例如
    • 因为这会在图像周围留下按钮镶边,所以我只想要图像。
    • 可以通过自定义模板从按钮中删除镶边...但是将图像添加为 。也许?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多