1

I am trying to bind ListView with TestRow class as below

  <Grid Background="White">
        <ListView ItemsSource="{Binding BindingTest}" HorizontalAlignment="Left" Height="176" Margin="49,41,0,0" VerticalAlignment="Top" Width="197">
            <ListView.View>
                <GridView>
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>


  public ObservableCollection<TestRow> BindingTest
         => ListToObservableCollection<TestRow>.ObservableCollectionFromList(new List<TestRow>
         {
                new TestRow(1, "jack"),
                new TestRow(2, "mark")
         });

After start ListView shows "XXXX.TestoRow" twice. However when I am binding List<int> it is working corret. How then I can bind my own objects to ListView?

1 Answer 1

3

Use DisplayMemberPath. If TestRow has fields:

class TestRow
{
  public int Index {get;set;}
  public string Name {get;set;}
}

then use

    <ListView ItemsSource="{Binding BindingTest}" DisplayMemberPath="Name" HorizontalAlignment="Left" Height="176" Margin="49,41,0,0" VerticalAlignment="Top" Width="197">

and selected values would be TestRow.

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.