2011-09-28 63 views
1

考虑以下代码:的DataTemplate问题(指定值时,属性)

public class customlistbox : ListBox 
{ 
    public customlistbox() 
    { 
     this.itemtemplate = this.gettemplate(); 
    } 

    private string gettemplate() 
    { 
     return ".......<locall:CustomGrid Background='" + Background.Color + "' ParentReference='" + this + "' > "....." 
    } 
} 

'ParentReference' 是它的类型是customlistbox的属性。但我给ParentReference ='“+这个+”',它显示错误。我怎么能在这种情况下给'ParentReference'带来价值?

回答

0

这是失败的,因为你传递一个字符串到一个属性是DataTemplate

您可以尝试使用XamlReader将字符串加载为xaml,但为了仅在模板中指定颜色,我会在XAML中定义模板,然后将颜色值绑定到该颜色。