2016-04-08 63 views
0

我已經得到了與項目結合,看起來像這樣一個ListView:如何自定義項添加到ListView與集合結合

<ListView x:Name="DetailsAZList" ItemsSource="{Binding AZEntries.AZEntries, Mode=OneWay}" > 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Border Background="LightGray" BorderBrush="DimGray" BorderThickness="2" CornerRadius="0" Margin="0 2 0 10"> 
       <Grid MinHeight="140" HorizontalAlignment="Stretch"> 
        <StackPanel Margin="10 0" Orientation="Vertical"> 
         <TextBlock Text="{Binding ToDoBezeichnung, Mode=OneWay}"/> 
         <!--UserName Unter umständen auch anzeigen, falls Arbeitszeiten anderer User mit aufgeführt werden--> 
         <!--<TextBlock Text="{Binding UserName, Mode=OneWay}"/>--> 
         <TextBlock Text="{Binding start, Mode=OneWay}"/> 
         <TextBlock Text="{Binding finished, Mode=OneWay}"/> 
         <TextBlock Text="{Binding Kostenpflichtig, Mode=OneWay}"/> 
         <controls:WrapPanel Orientation="Horizontal" BlockSize="40"> 
          <!-- Example from: stackoverflow.com/questions/28223693/how-to-bind-buttons-in-listview-datatemplate-to-commands-in-viewmodel-mvvmligh --> 
          <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.PlayCommand}" 
            CommandParameter="{Binding}" 
            Margin="0 0 10 0"> 
           <SymbolIcon Symbol="Play"/> 
          </Button> 
          <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.PauseCommand}" 
            CommandParameter="{Binding}" 
            Margin="0 0 10 0"> 
           <SymbolIcon Symbol="Pause"/> 
          </Button> 
          <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.StopCommand}" 
            CommandParameter="{Binding}" 
            Margin="0 0 10 0"> 
           <SymbolIcon Symbol="Stop"/> 
          </Button> 
          <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.EditCommand}" 
            CommandParameter="{Binding}" 
            Margin="0 0 10 0"> 
           <SymbolIcon Symbol="Edit"/> 
          </Button> 
          <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.DeleteCommand}" 
            CommandParameter="{Binding}" > 
           <SymbolIcon Symbol="Delete"/> 
          </Button> 
         </controls:WrapPanel> 
        </StackPanel> 
       </Grid> 
      </Border> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="Margin" Value="0 2 0 0"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
</ListView> 

想什麼,我做的,是添加最上面的項目,它是未綁定的,也不使用數據模板,像這樣:

<ListView x:Name="DetailsAZList" ItemsSource="{Binding AZEntries.AZEntries, Mode=OneWay}" > 

    <!-- Here comes the relevant Item --> 
    <ListViewItem x:Name="TopmostUnboundItem"> 
     <Grid HorizontalAlignment="Strech" Background="LightGray"> 
      <TextBox Text="Add Entry"/> 
     </Grid> 
    </ListViewItem> 
    <!-- Thats it --> 

    <ListView.ItemTemplate> 
     <DataTemplate> 
      <Border Background="LightGray" BorderBrush="DimGray" BorderThickness="2" CornerRadius="0" Margin="0 2 0 10"> 
       <Grid>...</Grid> 
      </Border> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="Margin" Value="0 2 0 0"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
</ListView> 

這在某種程度上可能,而不必到我指定Collection改變的ObservableCollection添加的項目,然後用TemplateSelector?我希望避免這種情況,因爲我覺得這對於只有一個物品會有點矯枉過正。

回答

1

使用Header並將您的內容輸入ListView.HeaderTemplate。既然你只想放置1件物品,不需要改變你的收藏。

+0

我應該說,我需要從ListViewItem的視覺和邏輯點擊行爲。由於Listview.Header本身不支持,所以它不是一個選項。現在我的解決方法是使用自定義按鈕將其放置在ListView上方,這對於目前來說是可行的。我可能會將按鈕放在標題中。 –

+1

您可以將ListViewItem放入標題模板中。它會有你想要的所有東西ListViewItem – thang2410199

+0

你有點兒天真,對嗎? –