2

I have a class with data:

public class routedata : INotifyPropertyChanged

{

    private List<double> distances;
    public List<double> Distances
    {
        get { return this.distances; }
        set
        {
            if (this.distances != value)
            {
                this.distances = value;
                this.onPropertyChanged("Distances");
            }
        }
    }

    private List<string> instructions;
    public List<string> Instructions
    {
        get { return this.instructions; }
        set
        {
            if (this.instructions != value)
            {
                this.instructions = value;
                this.onPropertyChanged("Instructions");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void onPropertyChanged(string property)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }        
}

I'm trying to bind it to a listview like this:

     <GridView Name="routeView" HorizontalAlignment="Left" Height="310" Margin="1025,318,0,0" Grid.Row="1"      
           VerticalAlignment="Top" Width="340" >
            <ListView Name="routeList" Height="300" Width="330" ItemsSource="{Binding routeData}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Instructions}"
                                                      TextWrapping="Wrap" Width="200"/>
                            <TextBlock Text="{Binding Distances}"
                                                      Margin="10,0,0,0" />
                            <TextBlock Text="km"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </GridView>

I have in my c# code behind: routeList.datacontext = this;

but it is still not binding, only one empty row is populated in the listview. I have checked the data and it is all present. Any help would be appreciated thank you.

4
  • you have a single instance of routedata? or a list of routedata elements? Your GridView ItemsSource implies the latter. ItemSource should be set to a collection, and then in your template you assign UI elements to individual members of the class in that collection. You seem to have an inverted scenario. Commented Jan 26, 2013 at 21:41
  • I see. I have a single instance of route data. Should I just place the listview inside a grid? I tried this before and it still did not work. Thank you for response. Commented Jan 26, 2013 at 21:56
  • your route element is a single object and you have two different collections in it, but you're trying to pull both collections into a single grid, item by item. What would happen if the Instructions and Distances lists were different sizes? see @FilipSkakun answer below; you're data model is what needs a bit of modification and then the bindings will flow naturally. Commented Jan 26, 2013 at 22:13
  • I can guarantee that instructions and distances will always be the same. I got this working with an alternative method before but that was wasn't registering onselectionchanged handler properly. But am trying to implement the proper method atm. Commented Jan 26, 2013 at 22:47

2 Answers 2

1

A ListView takes a single collection as ItemsSource, so if you want to display multiple TextBlocks for each item - you need a collection of objects with multiple text properties to bind to your DataTemplate. In your case a routeData is not a collection. Instead you need to define your item view model, e.g.

public class RoutePoint
{
    public double Distance { get; set; }
    public string Instruction { get; set; }
}

then you would bind your ListView.ItemSource to a List and in your DataTemplate bind it like that:

<TextBlock Text="{Binding Distance}"/>
<TextBlock Text="{Binding Instruction}"/>

You don't need to use an ObservableCollection if your collection never changes after you bind it to the ListView for the first time (SelectedItem doesn't constitute a change).

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

2 Comments

Okay, i made the class like you said. I put it in an observable collection. I confirm the collection is populated with members and data. I set the datacontext of listview to the collection (i also tried setting it to "this"). And i set item source of listview to the collection. Still no go. Only thing i change in my data template was the name Instruction and Distance. It is still within a grid view. Stubborn thing is going to kill me.
What is your DataContext? Have you tried simply assigning the collection to ItemsSource in code behind? Can you share more of your code?
0

If your view is called routeView, shouldn't your DataContext be set to a new instance of routedata? Also, I suggest you use an ObservableCollection<T> for your bindable collections rather than List<T>.

1 Comment

I tried your suggestions (observable collection and fiddling with datacontext) but it is still not displaying anything. Only a single row in the list view.

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.