2012-01-26 55 views
0

我在資源下面XAML代碼:DataTemplate沒有顯示?

<DataTemplate DataType="{x:Type s:Substance}"> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="{Binding Name, Mode=TwoWay}" MinWidth="50" MinHeight="20" Background="Blue"/> 
     <TextBox Text="{Binding Count, Converter={StaticResource stringToIntConverter}, Mode=TwoWay}" MinWidth="50" MinHeight="20" Background="Yellow"/> 
    </StackPanel> 
</DataTemplate> 

SubstanceContentControl導出:

public partial class Substance : ContentControl 
{ 
    string name; public int count; SymbolTable symTable = null; 
    public Substance(string _name, int _count, SymbolTable _symTable) 
    { 
     symTable = _symTable; Name = _name; Count = _count; 
    } 
} 

NameCount是另一局部類定義中所定義的DPS。

當我將在StackPanelListBox什麼物質被所示:

Substance s = new Substance("newSub", 100, symTable); 
substancePanel.Children.Add(s); 

誰能告訴我什麼,我做錯了。任何幫助,將不勝感激。

回答

2

我不是告訴你不要讓Substance從UI相關的類繼承嗎?

如果您忽視DataTemplates將不會應用(取決於預期類型),但更糟糕的是,您打破了模型 - 視圖分離。

+0

Hrrrm我甚至沒有注意到繼承 – Rachel

+0

@Rachel:當我打開這個問題時,我期待它:P –

+0

@ H.B。你是對的,道歉。它在我添加到ListBox時起作用,但是當我將它添加到面板中時,它不會顯示,如果我想同時實現,我應該從ContentControl繼承並提供一個ControlTemplate(作爲附註,控件不能同時擁有一個DataTemplate和一個Controltemplate吧? – mihajlv