2011-09-28 37 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中定義模板,然後將顏色值綁定到該顏色。