WPF ContentControl没有显示任何内容。

3

我正在尝试构建一个控件,根据传入的类型有选择地显示不同的内容,但出现了一些问题,最终导致什么也没有显示。

我是否遗漏了某些基本的东西?(这段代码已经被大量简化,但仍然表现出相同的行为)

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>

我不知道"{Binding /}"的意思,但我不使用它。只需将其更改为"{Binding}" - Federico Berasategui
我从未见过绑定使用像你现在这样的 / 符号...也许这有关系?一个快速的测试方法是将你的 DataTemplates 移到 <Window.Resources> 中,并将你的 ItemsControl 设为 <ItemsControl ItemsSource="{Binding }" />。默认情况下,ItemsControl 已经用 ContentPresenter 绘制了你的项,并应该使用隐式模板来绘制每个项。如果这行不通,请使用诸如 Snoop 这样的工具检查你的可视树和它的 DataContext,那可能会指明正确的方向。 - Rachel
/代表集合的当前项,所以<ItemsControl ItemsSource="{Binding /}">表示ContactInformation列表的当前项。但是你不需要其他的/。 - Phil
@Phil 如果是这样的话,那么问题可能是因为当前项没有“当前项”属性,所以“/”不能用于嵌套绑定。我相信一定有一些绑定错误被输出,可以指出确切的问题位置 :) - Rachel
2个回答

4

您只需要删除两个斜杠,如下所示:

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

希望这可以帮到你。

不确定我有什么区别,但这给了我所需的工作部件,谢谢。 - Matt
@Matt 我正在尝试让XamDataGrid字段使用ContentControl,我设置ContentTemplate基于值,并且与您具有相同的结果,什么也没有出现。没有错误,但也没有网格。我已经在标准WPF DataGrid中使其全部工作。但是无法使其在XamDataGrid中工作。我在这篇文章中看不到任何可以使其工作的东西。你能告诉我你是如何让它工作的吗? - user2109254

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>

我将尝试从视图模型中提取项目。真正的生产代码并没有使用items控件。我正在做的是填充Infragistics XamDataGrid的单个单元格。ItemsControl表现出相同的行为,这就是为什么我将其用作示例的原因。 - Matt
据我所见,您的代码与我的没有显示任何不同的结果。一切都还是空的。 - Matt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接