简体   繁体   中英

Display data in WPF Listview

I am developing wpf application, in which i have listview control. I am very new to wpf. But managed to add some styles and display horizontal gridlines inside Listview as below,

 <DataTemplate x:Key="IDBorderedCellTemplate">

                <Border BorderBrush="Black" Margin="0,5,0,1"

                BorderThickness="0,0,0,1" >

                    <TextBlock Foreground="MediumBlue" FontFamily="Calibri" Margin="3,0,0,2" Text=""
                />

                </Border>
            </DataTemplate>


 <ListView.View>

                <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">

                    <!-- Product ID -->

                    <GridViewColumn



          CellTemplate="{StaticResource IDBorderedCellTemplate}">

Now my problem is i am uable to display items in listview when window is loaded, which is displaying only gridlines. As my requirement is i have a browse button which i browse files, these browsed files must be displayed in listview.

As in load event i am adding following lines of code,

    try
    {

        listviewitem = new ListViewItem();
        //displays only the filename in the listview
        listviewitem.Content = System.IO.Path.GetFileName(_name);
        _listFiles.Items.Add(listviewitem);
    }
    catch (Exception)
    {
   }

This data is not displaying in listview after adding the gridview settings in xaml. Now its displays only gridlines. Any suggesions/help is appreciable.

Regards Ravi

Choose the ItemsSource of listview as ObservableCollection or BindingList for dynamic/frequently changing data instead of using listviewItem.

XAML

  <ListView  Name="listView1" ItemsSource="{Binding FileStore}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="120" Header="Files" DisplayMemberBinding="{Binding}" />
        </GridView>
    </ListView.View>
 </ListView>

C#

 public partial class MainWindow : Window
 {
   public ObservableCollection<string> fileList= new ObservableCollection<string>();

    public MainWindow()
    {
       InitializeComponent();
        this.DataContext = this;
       // Add files to fileList (ObservableCollection)  
    }

    public ObservableCollection<string> FileStore
   {
      get { return fileList; }
   } 
  }

Can add items to ObservableCollection anywhere like browse button click or load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM