2015-09-07 27 views
0

我最近開始使用XAML和WPF。我剛剛在wpf中創建了一個新項目並添加了下面的XAML代碼。但是......在「Listbox.ItemTemplate」或「ListView.ItemTemplate」內部添加的項目都不出現在設計器窗口中。我究竟做錯了什麼?這是一個新鮮的項目,所以沒有添加任何代碼隱藏的東西。我用這個劃傷了我的頭15分鐘,但沒有成功。請幫助WPF控件不能顯示在ItemTemplate(Listbox/ListView)

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Grid Margin="10"> 
     <ListBox Margin="10"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="Name: " /> 
         <TextBlock Text="Age: " /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox>  
    </Grid> 
</Window> 

enter image description here

+1

嗯,這是因爲'ListBox'沒有任何*項*要顯示。 – Dennis

+0

您如何將一些項目添加到您的列表框中,然後檢查 – Rohit

+0

@Dennis是不是TextBlock具有文本屬性值的項目? – 911Rapid

回答

4

你應該結合你的列表框或整個窗口的一些DataContext的(通常這是視圖模型不同,需要顯示的數據)或明確指定列表中的項目。

在您的片段中,您只指定了一個項目模板,而不是項目本身。

的XAML定義的項目的例子(簡單的字符串):

<Window x:Class="WpfApplication3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 

    <Grid Margin="10"> 
     <ListBox Margin="10"> 
      <ListBox.Items> 
       <ListBoxItem>123</ListBoxItem> 
       <ListBoxItem>456</ListBoxItem> 
      </ListBox.Items> 
     </ListBox>  
    </Grid> 
</Window> 

用的DataContext和綁定的示例。 XAML:

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Grid Margin="10"> 
     <ListBox Margin="10" ItemsSource="{Binding Path=Persons}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <Label>Name:</Label><TextBlock VerticalAlignment="Center" Text="{Binding Path=Name}" /> 
         <Label>Age:</Label><TextBlock VerticalAlignment="Center" Text="{Binding Path=Age}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox>  
    </Grid> 
</Window> 

代碼隱藏:

namespace WpfApplication3 
{ 
    public class PersonViewModel 
    { 
     public PersonViewModel(string name, int age) 
     { 
      this.name = name; 
      this.age = age; 
     } 

     public string Name 
     { 
      get { return name; } 
     } 
     private string name; 

     public int Age 
     { 
      get { return age; } 
     } 
     private int age; 
    } 

    public class MainViewModel 
    { 
     public MainViewModel() 
     { 
      persons = new ObservableCollection<PersonViewModel>() 
      { 
       new PersonViewModel("Lez", 146), 
       new PersonViewModel("Binja", 158), 
       new PersonViewModel("Rufus the Destroyer", 9000) 
      }; 
     } 

     public ObservableCollection<PersonViewModel> Persons 
     { 
      get { return persons; } 
     } 

     private ObservableCollection<PersonViewModel> persons; 
    } 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      this.DataContext = new MainViewModel(); 
     } 
    } 
} 

重要:對於「名稱不要忘記在的ViewModels的可變特性的情況下,適當地執行INotifyPropertyChanged(例如,你是否有制定者「和PersonViewModel的」Age「屬性)。

+1

這將是很好的添加一些樣品,如何項目可以宣佈和/或綁定到控制。 – Dennis

+1

@丹尼斯完成! XAML和DataContext都是這種情況。 – Nipheris

+1

爲了在設計器中顯示項目,在xaml中實例化視圖模型,或者使用'd:DataContext =「{d:DesignInstance l:MainWindowViewModel,IsDesignTimeCreatable = True}'' – Liero

1

您看不到任何項目,因爲您的ListBox在設計器視圖中不包含任何數據。爲了填充它,列表應綁定到ListBox的屬性「ItemsSource」,或直接將數據添加到屬性「Items」(XAML或後面的代碼)。

如果你想正確地在設計器視圖中顯示項目,你應該有一個ViewModel綁定到你的頁面的DataContext並創建一個「示例數據」文件(例如通過Blend)。

0

項目模板基本上用於顯示自定義數據。請先使用簡單的字符串項目。它將顯示在列表框中。