2017-07-30 67 views
0

我想ListView與包含一些信息的擴展器。所以,我做了這個代碼。我不確定這樣的綁定擴展器是否正確。我只是嘗試綁定像ListViewItem,但是當我嘗試擴展器根本不工作。這是我的代碼。WPF:在列表視圖中的擴展器綁定

XAML:

<Grid Grid.Row="2"> 
     <ListView x:Name="lv"> 
      <ListView.Template> 
       <ControlTemplate> 
        <HeaderedItemsControl> 
         <ItemsPresenter/> 
        </HeaderedItemsControl> 
       </ControlTemplate> 
      </ListView.Template> 

      <ListView.ItemTemplate> 
       <DataTemplate DataType="{x:Type local:LogBase}"> 
        <Expander Grid.Column="0" HorizontalAlignment="Center"> 
         <Expander.HeaderTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal"> <!-- why this code is not wokring...? --> 
            <TextBlock Text="{Binding No}"/> 
            <TextBlock Text="{Binding Timestamp}"/> 
            <TextBlock Text="{Binding Type}"/> 
           </StackPanel> 
          </DataTemplate> 
         </Expander.HeaderTemplate> 
        </Expander> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </Grid> 

後面的代碼:

public partial class MainWindow : Window 
{ 
    public List<LogBase> logs { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     logs = new List<LogBase>(); 

     logs.Add(new LogBase() 
     { 
      No = "1", 
      Timestamp = "123456789", 
      Type = "Tcp" 
     }); 

     logs.Add(new LogBase() 
     { 
      No = "2", 
      Timestamp = "123456789", 
      Type = "Tcp" 
     }); 

     logs.Add(new LogBase() 
     { 
      No = "3", 
      Timestamp = "123456789", 
      Type = "Tcp" 
     }); 


     lv.ItemsSource = logs; 

     DataContext = this; 
    } 

} 

public class LogBase 
{ 
    public string No { get; set; } 
    public string Timestamp { get; set; } 
    public string Type { get; set; } 
} 

更好地瞭解我拍攝我想要

Now my program's situation

如果您有任何意見,請評論爲了我!

回答

0

您還需要綁定標頭以正確設置DataContextHeaderTemplate。這由Header="{Binding HeaderSource}"完成。在你的情況下,只需使用Header="{Binding}"直接綁定到該項目:

<Expander Header="{Binding}> 

之後,你的代碼完美的作品。

+0

非常感謝你......我感覺就像在雲下一樣。我錯過了什麼......但是..只是答案是Header =「{Bining}」.. –

+0

你能回答爲什麼標題沒有綁定Listview.ItemTemplate作爲默認?我無法找到在擴展器中的MSDN –

+0

看看https://stackoverflow.com/questions/26242734/what-does-exactly-expander-header-binding-do – Fruchtzwerg