WPF ContentControl没有显示任何内容

时间:2013-04-26 18:41:11

标签: wpf datatemplate contentcontrol

我正在尝试构建一个Control,它根据传入的类型有选择地显示不同的东西,但由于某种原因,我最终都没有显示任何内容。

我在这里缺少一些基本的东西吗? (此代码从我的真实生产应用程序中大量剥离,表现出相同的行为)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new List<ContactInformation>
            {
                new Address {Street = "21 Jump", City = "Sparta", State = "Denial"},
                new Phone {Number = "734-555-1212"}
            };
    }
}

public class ContactInformation
{
}

public class Address : ContactInformation
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

public class Phone : ContactInformation
{
    public string Number { get; set; }
}

<Window x:Class="ContentControlExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:contentControlExample="clr-namespace:ContentControlExample"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ItemsControl ItemsSource="{Binding /}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl DataContext="{Binding /}" Content="{Binding /}">
                        <ContentControl.Resources>
                            <DataTemplate DataType="{x:Type contentControlExample:Address}">
                                <StackPanel>
                                    <TextBlock Text="{Binding Street}"/>
                                    <TextBlock>
                                        <TextBlock.Text>
                                            <MultiBinding StringFormat="{}{0}, {1}">
                                                <Binding Path="City"/>
                                                <Binding Path="State"/>
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                </StackPanel>
                            </DataTemplate>
                            <DataTemplate DataType="{x:Type contentControlExample:Phone}">
                                <TextBlock Text="{Binding Number}"/>
                            </DataTemplate>
                        </ContentControl.Resources>
                    </ContentControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

2 个答案:

答案 0 :(得分:4)

您只需删除几个&#39; /&#39;如下:

XAML:

<Window x:Class="ContentControlExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:contentControlExample="clr-namespace:ContentControlExample"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ItemsControl ItemsSource="{Binding }">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl DataContext="{Binding }" Content="{Binding }">
                        <ContentControl.Resources>
                            <DataTemplate DataType="{x:Type contentControlExample:Address}">
                                <StackPanel>
                                    <TextBlock Text="{Binding Street}"/>
                                    <TextBlock>
                                        <TextBlock.Text>
                                            <MultiBinding StringFormat="{}{0}, {1}">
                                                <Binding Path="City"/>
                                                <Binding Path="State"/>
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                </StackPanel>
                            </DataTemplate>
                            <DataTemplate DataType="{x:Type contentControlExample:Phone}">
                                <TextBlock Text="{Binding Number}"/>
                            </DataTemplate>
                        </ContentControl.Resources>
                    </ContentControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

守则背后:

namespace ContentControlExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new List<ContactInformation>
            {
                new Address {Street = "21 Jump", City = "Sparta", State = "Denial"},
                new Phone {Number = "734-555-1212"}
            };
        }
    }

    public class ContactInformation
    {
    }

    public class Address : ContactInformation
    {
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
    }

    public class Phone : ContactInformation
    {
        public string Number { get; set; }
    }
}

输出:

Result

我希望这会有所帮助。

答案 1 :(得分:2)

尝试稍微更改您的代码。这是有效的,因为ItemsControl会根据所绑定项目的类型自动选择正确的DataTemplate

public class ViewModel
{
    public ViewModel()
    {
        this.Items = new List<ContactInformation>
                         {
                             new Address
                                 {
                                     Street = "21 Jump", 
                                     City = "Sparta", 
                                     State = "Denial"
                                 }, 
                             new Phone { Number = "734-555-1212" }
                         };
    }

    public List<ContactInformation> Items { get; set; }
}

<Window.DataContext>
    <contentControlExample:ViewModel/>
</Window.DataContext>
<Grid>
    <Grid.Resources>
        <DataTemplate DataType="{x:Type contentControlExample:Address}">
            <StackPanel>
                <TextBlock Text="{Binding Street}"/>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0}, {1}">
                            <Binding Path="City"/>
                            <Binding Path="State"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type contentControlExample:Phone}">
            <TextBlock Text="{Binding Number}"/>
        </DataTemplate>
    </Grid.Resources>

    <ItemsControl ItemsSource="{Binding Items}"/>
</Grid>

或者将当前项目绑定到内容控件:

<Grid>
    ... resources

    <ContentControl Content="{Binding Items/}"/>
</Grid>