2011-03-21 95 views
2

我已经实现了一个控件CommandTextBox,我想成为一个文本框,旁边有一个按钮(因此它几乎出现在文本框中)。TemplateBinding在Silverlight 4中的嵌套模板

该按钮应该是一个图像,我可以绑定到一个图标。这是相当straightfoward东西...

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控件,以便我可以执行普通的模板绑定或有其他方法?

感谢 本

回答

2

我设法得到这种通过改变风格如下工作:

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

这看起来可以达到您以后的效果。控件按预期在我的测试页上呈现。

+0

这不是数据绑定问题,我还没有ViewModel。这是一个控件,模板绑定将模板中的值设置为控件上属性的值。 – BenCr 2011-03-21 14:31:52

+0

好吧,我不确定你为什么在你的风格中使用独立的Button.Template。我有一个测试控件只是通过在我的样式中使用Button.Content,例如 2011-03-21 15:30:07

+0

因为在图像周围留下按钮镀铬物,我只想要图像。 – BenCr 2011-03-21 15:52:57