2013-04-26 129 views
2

我想建立一个控件,根据传入的类型选择性地显示不同的东西,但由于某种原因,我最终没有显示任何内容。WPF ContentControl不显示任何东西

我在这里失踪了一些基本的东西吗? (这段代码从我的真实生产应用程序中被大规模剥离出来,展现出相同的行为)

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> 
+0

我不知道''{Binding /}“是什么意思,但我没有使用它。只需将其更改为“{绑定}”。 – 2013-04-26 18:54:52

+0

我从来没有见过绑定使用'/'就像你有......也许这是相关的?一种快速的测试方法是将DataTemplates移动到''并将Your ItemsControl保存为'。默认情况下,ItemsControl已经用ContentPresenter绘制了你的项目,并且应该使用隐式模板来绘制每个项目。如果这不起作用,请使用[Snoop](http://snoopwpf.codeplex.com/)这样的工具查看您的可视化树,并且它是'DataContext',这可能会指向正确的方向。 – Rachel 2013-04-26 18:55:18

+0

/表示集合的当前项目,因此将表示ContactInformation列表的当前项目。你不需要其他的。 – Phil 2013-04-26 18:57:03

回答

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

我希望这会有所帮助。

+0

没有试过,没有区别。 – Matt 2013-04-26 19:34:16

+0

嗨马特。我会发布整个代码和输出。给我一个秒... – FHnainia 2013-04-26 19:36:41

+0

不知道我有什么不同,但这给了我需要感谢的工作件。 – Matt 2013-04-26 20:24:37

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> 
+0

我会尝试拉出视图模型上的项目。真正的生产代码不使用项目控件。我真正在做的是填充Infragistics XamDataGrid的单个单元。 ItemsControl展现出同样的行为,这就是为什么我以此为例。 – Matt 2013-04-26 19:20:49

+0

尽我所能看到你的代码不会显示我的任何不同结果。一切仍然是空的 – Matt 2013-04-26 19:28:49