在列表框中加载datatemplate

时间:2014-06-13 14:38:18

标签: c# wpf xaml listbox datatemplate

我创建了一个datatemplate并将其用作列表框中的资源,但列表框并未显示我的数据模板。 这是datatemplate定义的代码

<Window.Resources>
    <DataTemplate x:Key="template1">
        <Canvas Height="40" Width="850">
            <TextBlock Height="40" Width="40" Canvas.Top="10" Foreground="Aqua">
            </TextBlock>

            <Label>hello</Label>
        </Canvas>
    </DataTemplate>
</Window.Resources>

并且列表框的代码在这里

<TabItem>
    <Canvas Height="700" Width="850">
        <ListBox Height="700" Width="850" ItemTemplate="{StaticResource template1}">
        </ListBox>
    </Canvas>
</TabItem>

我哪里错了???

1 个答案:

答案 0 :(得分:1)

1)如果您使用tabitem而没有tabcontrol,那么在将itemsource应用到Listbox后输出就不会显示。

2)如果你想显示datatemplate(数据的表示),那么你必须为listbox绑定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#c​​ode

   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>