开发者

Displaying items from a list onto the column in gridview

Im trying to display the name and path of a file in a listbox. For example, the name should come under H开发者_如何学JAVAeader FileName and path under FilePath. I do not want to bind to any xmls, as i have a code to display the file name and size.Im new to this and im not sure how to go about this. Thanks!


I'm not sure how to help you without seeing any of your code or structure of the data you are trying to bind but I'll give it a shot.

Let's say you're trying to bind the names and paths of files in C:\MyFolder directory and your grid view has a name grd_MyGrid:

string[] myFiles = Directory.GetFiles("C:\\MyFolder\\");

var files = from f in myFiles
                        select new{
                                    FileName =Path.GetFileName(f),
                                    FilePath = Path.GetPathRoot(f)
                                  };

grd_MyGrid.DataSource=files;

In order for this to work, you have to reference System.Linq.

Hope this helps.


To start you off, I will supply some code, but you really should read up on at least some of the basics when it comes to XAML and WPF for future development tasks.

If you can do without the ListBox, I would suggest using a DataGrid (in .Net 4.0 - or in the WPF Toolkit on CodePlex). The DataGrid is easier to use in situations where you want to display data in a grid or report.

To create a DataGrid in XAML, you can use the following code (in .net 4)

<DataGrid HorizontalScrollBarVisibility="Auto" ItemsSource="{Binding Path=ItemsToDisplay}" IsReadOnly="True" AutoGenerateColumns="True" />

This will create a simple DataGrid object for you to display on screen. Because AutoGenerateColumns has been set to true, your columns will be created for you automatically by the control.

You may also notice that I have set the ItemsSource property, this is the property that the DataGrid will get it's items from.

To define this in the Page code-behind file, you will be able to do something like this:

public System.Collections.ObjectModel.ObservableCollection<Item> ItemsToDisplay { get; private set; }

Notice how the name of the property in the code-behind matches the name of the property in the Binding on the DataGrid. This is how the View (page file) links to the ViewModel (code-behind)

To test it out, create a simple test class and populate the ItemsToDisplay collection with items.

For example:

In my MainWindow.xaml.cs file

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        ItemsToDisplay = new System.Collections.ObjectModel.ObservableCollection<Item>();

        ItemsToDisplay.Add(new Item("Homer", 45));
        ItemsToDisplay.Add(new Item("Marge", 42));
        ItemsToDisplay.Add(new Item("Bart", 10));
        ItemsToDisplay.Add(new Item("Lisa", 8));
        ItemsToDisplay.Add(new Item("Maggie", 2));
    }

    public System.Collections.ObjectModel.ObservableCollection<Item> ItemsToDisplay { get; private set; }
}

public class Item
{
    public string Name { get; private set; }
    public int Age { get; private set; }

    public Item(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

And in my MainWindow.xaml file:

<Window x:Class="Stackoverflow.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>
        <DataGrid HorizontalScrollBarVisibility="Auto" ItemsSource="{Binding Path=ItemsToDisplay}" AutoGenerateColumns="True" IsReadOnly="True" />
    </Grid>
</Window>

Which looks like:

Displaying items from a list onto the column in gridview

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜