1

I have this template for my GridView:

  <DataTemplate x:Key="GroupTemplate1">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,9.5,0,0">
                <Image Source="{Binding Property3}" Height="79" Width="79"/>
            </Border>

            <!-- <StackPanel Grid.Column="1" Margin="14.5,0,0,0">
                <TextBlock Text="{Binding Property1}" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                <TextBlock Text="{Binding Property2}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"/>
            </StackPanel> -->

        </Grid>
    </DataTemplate>

As you can see, for each item there are three infos: Property3 (Image), Property2 (text), Property1 (text).

How can create and add a new ListViewItem with those three fields and add it to the GridView programmatically?

Something like:

//
gridView1.Add(new ListViewItem("imagename", "text1", "text2");
//
2
  • 1
    Is there a particular reason you're not wrapping the GridView in a ListView and setting the ItemsSource of the ListView to a Collection of the items that match GroupTemplate? This is the kind of problem that MVVM is well-suited to solving with no headache and no need to add things programmatically to the UI. Commented Oct 26, 2014 at 16:27
  • Because I have to change the images of the grid view during the execution of the app Commented Oct 27, 2014 at 12:11

1 Answer 1

2

It seems like you're trying to do MVVM and non-MVVM at the same time. You can't really do things the way you are above, with a pre-made DataTemplate and binding. In general, you've mostly got a choice between pre-constructing the ListView in XAML, in which case the design is more or less static, or binding the ListView to a collection and using INotifyPropertyChanged to get the UI to recognize that it needs to update the view. Understand that much of the power of WPF comes from seldom ever needing to manually add items to UI controls or to alter them at all.

Here's a very simple implementation:

This would be the object from which each GridViewItem would take its information - where Property1, Property2, and etc. will come from:

class ModelSample
{
    public object Property1 { get; set; } // anything you ever bind to *must* be a public property
    public object Property2 { get; set; }
    public object Property3 { get; set; }
}

And here is an implementation of a sample ViewModel:

class ViewModelSample : INotifyPropertyChanged  // uses System.ComponentModel
{
    private List<ModelSample> models = new List<ModelSample>();
    public List<ModelSample> Models 
    { 
        get { return models; }
        set
        {
            models = value;
            NotifyPropertyChanged("Models");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }   
}

Then you'd instantiate the ViewModelSample and set the DataContext:

var viewModelSample = new ViewModelSample();
// add your objects to viewModelSample.Models
this.DataContext = viewModelSample.

And bind the ItemsSource property on your ListView to Models. Any time you want to change the images of the ListView, just modify the contents of Models and the UI should immediately reflect the current state of the collection.

This may seem a little more complicated than you're used to, but once you get the basic idea of using NotifyPropertyChanged to alert the UI, you'll find it's actually a whole lot less work, and WPF takes care of nearly everything for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.