tags:

views:

50

answers:

2

Hi, I am writing a WPF application where where i need to display custom file iformation which consists of field name & its value. I generate a grid rumtime with label & textboxes. I display the field name in label & field value in textbox(i want it to be editable). & each time file selection changes, number of field change & so the grid columns & rows. Right now I am generating this grid in code behind . Is there any way i can do it in XAml with view model.

A: 

I'm not sure why you're creating this grid at runtime. You should look into using a standard presentation method such as a <ListBox> with a custom item template. Always look to use declaritive definition of your UI (within the XAML) instead of the codebehind.

I've got a blog post on creating a checked listbox that shows some of the details, but you should be able to find other good examples out there as well.

Robaticus
A: 

This is pretty easy to do with an ItemsControl. If you ViewModel exposes a list of metadata objects, say a class like this:

public class FileMetaData : INotifyPropertyChanged
{
    private string name;
    private string value;

    public event PropertyChangedEventHandler PropertyChanged = (o, e) => { };

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    public string Value
    {
        get { return value; }
        set
        {
            this.value = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Value"));
        }
    }
}

Then, your ViewModel would expose it as an ObservableCollection (so WPF knows when new items are added or removed):

public class MyViewModel
{
   ... 
   public ObservableCollection<FileMetaData> Files { get; private set; }
   ...
}

Then, your view would use an ItemsControl with an ItemTemplate to display it:

<ItemsControl ItemsSource="{Binding Files}" Grid.IsSharedSizeScope="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="one" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="two" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" />
                <TextBox Grid.Column="1" Text="{Binding Value}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Note that I'm setting Grid.IsSharedSizeScope to true on the ItemsControl, so the columns will align. If you have a lot of data, you'll probably want to wrap this in a ScrollViewer (or better retemplate the ItemsControl to have one).

Abe Heidebrecht
Thanks for your ans. This looks ok but another requirement is that it wont be displayed as one column only but depending on fields we ll decide runtime how many columns grid will have, basically to spread it horizotally. so smtime column count cms as 5 or 4 ,whatever. So is it possible to add columndefination runtime to datatemplate? (pls forgive if u find this very basic, I am new to WPF)
Rik
This will generate a number of rows that have the Label / TextBox entries (because the default panel used for an ItemsControl is a vertical StackPanel). If you want to switch this around so that each item is arranged horizontally, you would need to set the ItemsControl.ItemsPanel to a StackPanel with horizontal orientation. Dr. WPF has a great series explaining ItemsControl over at his blog. Here is a link to the first article: http://drwpf.com/blog/2007/10/15/itemscontrol-a-is-for-abundance/
Abe Heidebrecht
gr8, I ll try this. Thanks for the help !!! i relly appriciate it.
Rik