Search code examples
c#wpfxamldatatemplatestackpanel

DataTemplate doesn't get shown?


I have the following xaml code in resources:

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

Substance derives from ContentControl:

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

Name and Count are DPs defined in another partial class definition.

When I added a substance in a StackPanel or ListBox nothing gets shown:

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

Can anyone tell me what I am doing wrong. Any help would be appreciated.


Solution

  • Didn't i tell you not to make Substance inherit from a UI-related class?

    If you disregard that DataTemplates will not be applied (depending on expected type), but even worse than that you break the model-view-separation.