Unable to bind ObservableCollection from MVVM to ListView in WPF

I am trying to bind a List view to an observable collection of a class, and for some reason the DisplayMemberBinding attribute of the GridViewColumn is not required for the contents of the collection. If I use ListView.ItemTemplate, everything works fine. But I need grid data, so I use GridView inside ListView.View.

The first image, image 1, is the content of the AssessmentSummaryList in debug mode. The second image, image 2, is the final result shown on the screen. Listed content is optional for GridViewColumn.

These are the two viewing models that I use:

public class AssessmentSummaryViewModel : BaseViewModel
{
    public string Question { get; set; }

    public string Title { get; set; }

    public string SelectedOption { get; set; }

    public int Id { get; set; }
}

public class AssessmentViewModel : BaseViewModel 
{
    private ObservableCollection<AssessmentSummaryViewModel> assessmentSummaryList;

    public ObservableCollection<AssessmentSummaryViewModel> AssessmentSummaryList
    {
        get { return assessmentSummaryList; }
        set
        {
            assessmentSummaryList = value;
            NotifyPropertyChanged("AssessmentSummaryList");

        }
    } 

    public void SetNextAssessment()
    {
       AssessmentSummaryList= Service.GetAssessmentSummary(ApplicationModel.SelectedModule.Id,    
         ApplicationModel.SelectedUtility.Id); //the service returns ObservableCollection<AssessmentSummaryViewModel> data
      var EndScreen = new AssessmentSummaryView(); //Setting the last screen to AssessmentSumamryView, this will be called dynamically from other UserControl Xaml Page
    }
}

The SAMMaryView Xaml Code score is as follows:

<ListView x:Name="lstvSummary1" Grid.Row="2" ItemsSource="{Binding Path=DataContext.AssessmentSummaryList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<ListView.View>
            <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
                <GridViewColumn DisplayMemberBinding="{Binding Path=Title}" Width="200">
                    <GridViewColumnHeader Content="Design Element" />
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Question}" Width="400">
                    <GridViewColumnHeader Content="Question" />
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding Path=SelectedOption}" Width="300">
                    <GridViewColumnHeader Content="Response" />
                </GridViewColumn>
            </GridView>
        </ListView.View>
</ListView>

The CodeSummaryView score is as follows:

public partial class AssessmentSummaryView : UserControl
{
    public AssessmentSummaryView()
    {
        InitializeComponent();
    }
}
+4
1

- :

<ListView ItemsSource="{Binding Path=DataContext.AssessmentSummaryList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<ListView.View>
    <GridView>
        <GridView.Columns>
            <GridViewColumn Header="Design Element" Width="200" DisplayMemberBinding="{Binding Path=Title}"/>
            <GridViewColumn Header="Question" Width="400" DisplayMemberBinding="{Binding Path=Question}"/>
            <GridViewColumn Header="Response" Width="300" DisplayMemberBinding="{Binding Path=SelectedOption}"/>
        </GridView.Columns>
    </GridView>
</ListView.View>

0

Source: https://habr.com/ru/post/1530518/


All Articles