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控件,以便我可以执行普通的模板绑定或有其他方法?
感谢 本
这不是数据绑定问题,我还没有ViewModel。这是一个控件,模板绑定将模板中的值设置为控件上属性的值。 – BenCr 2011-03-21 14:31:52
好吧,我不确定你为什么在你的风格中使用独立的Button.Template。我有一个测试控件只是通过在我的样式中使用Button.Content,例如 Button.Content> –
2011-03-21 15:30:07
因为在图像周围留下按钮镀铬物,我只想要图像。 – BenCr 2011-03-21 15:52:57