1) tabcontrol없이 tabitem을 사용하는 경우 목록 소스에 itemsource를 적용한 후 출력이 표시되지 않습니다.
2) 데이터 형식 (데이터 표시)을 표시하려면 목록 상자에 itemsource를 바인딩해야합니다.
XAML 코드는
<Window.Resources>
<DataTemplate x:Key="template1">
<Canvas Height="40" Width="850">
<TextBlock Height="40" Width="40" Canvas.Top="10" Foreground="Aqua"></TextBlock>
<Label Content="{Binding State}"></Label>
</Canvas>
</DataTemplate>
</Window.Resources>
<TabControl>
<TabItem>
<Canvas Height="700" Width="850">
<ListBox Height="700" Width="850" ItemsSource="{Binding}" ItemTemplate="{StaticResource template1}">
</ListBox>
</Canvas>
</TabItem>
</TabControl>
C# 코드
public partial class MainWindow : Window
{
private ObservableCollection<City> cities = new ObservableCollection<City>();
public MainWindow()
{
InitializeComponent();
cities.Add(new City() { Name = "Boston", State = "MA", Population = 3000000 });
cities.Add(new City() { Name = "Los Angeles", State = "CA", Population = 7000000 });
cities.Add(new City() { Name = "Frederick", State = "MD", Population = 65000 });
cities.Add(new City() { Name = "Houston", State = "TX", Population = 5000000 });
DataContext = cities;
}
class City
{
public string State { get; set; }
public string Name { get; set; }
public int Population { get; set; }
}
}
3) 또한 ListBoxItem의 ContentTemplate을 디자인 할 수 있습니다.
<Window.Resources>
<Style x:Key="ListboxItem" TargetType="ListBoxItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Canvas Height="40" Width="850">
<TextBlock Height="40" Width="40" Canvas.Top="10" Foreground="Aqua"></TextBlock>
<Label Content="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}"/>
</Canvas>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<TabControl>
<TabItem Background="Red">
<ListBox Height="700" Width="850" ItemContainerStyle="{StaticResource ListboxItem}">
<ListBoxItem Content="Hello" Foreground="red"></ListBoxItem>
<ListBoxItem Content="Hello1"></ListBoxItem>
<ListBoxItem Content="Hello2"></ListBoxItem>
</ListBox>
</TabItem>
</TabControl>
ListBox에는 항목이 없습니다. 아무것도 표시되지 않습니다. 'ItemsSource' 속성을 관련 컬렉션에 바인드했는지 확인하십시오. –
데이터 템플릿의 텍스트 상자와 레이블을 표시하고 싶습니다. 어떻게 표시 할 수 있습니까? – Safwan