문제

Is it possible to use different templates for items in the ListBox in WPF basing on type of item?

Say, we have base class:

class BaseItem
{
}

And then two more:

class IntItem : BaseItem
{
     public int I { get; set; }
}

class StringItem : BaseItem
{
     public string S { get; set; }
}

Then, I may build an ObservableCollection of BaseItems and attach to the ListBox. I would like to display the text as a textbox and the integer as a tracker, for instance. Is it possible? If so, how can I do it?

도움이 되었습니까?

해결책

Of course this is possible.

Just define a DataTemplate for each Type in your ListBox Resources:

<DataTemplate DataType="{x:Type IntItem}">
    ....
</DataTemplate>
<DataTemplate DataType="{x:Type StringItem}">
    ....
</DataTemplate>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top