簡體   English   中英

綁定到 ListView 上的 ViewModel 什么也沒顯示,但在其他控件上工作正常

[英]Binding to ViewModel on ListView shows nothing but works fine on other controls

我正在嘗試將位於我的 ViewModel class 中的 ObservableCollection 綁定到使用 GridView 顯示行的 ListView,但我無法在列表中顯示我的數據。

出於測試目的,我放置了一個 TextBlock 並可以使用 Binding 成功顯示數據(但是只有當我首先在代碼后面執行DataContext = ViewModel時,在 XAML 中執行ViewModel.MyData才不起作用)

我注意到無論ItemsSource綁定到什么,列表中總是顯示隨機數量的空元素,我可以說,因為當我將鼠標放在 ListView 上時 hover 行突出顯示。 這個數字與我收藏的容量不符。

如果在后面的代碼中我手動將ItemsSource設置為我要顯示的集合,則會顯示正確數量的元素,但仍然沒有顯示數據。

XAML

<Page x:Class="GestionStockWPF.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:GestionStockWPF"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="MainPage"
      ShowsNavigationUI="False">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="13*"/>
            <RowDefinition Height="77*"/>
        </Grid.RowDefinitions>

        <!-- The Binding here seems to do nothing, there isn't the correct amount of rows in the app -->

        <ListView x:Name="listView"
                  HorizontalAlignment="Stretch"
                  Grid.Row="1" Margin="30, 0, 30, 30" 
                  ItemsSource="{Binding Source=Tests}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=A}"
                        Header="Asset Number" Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>

        <!-- This textblock is just here for testing purposes, it successfully shows "A" -->

        <TextBlock x:Name="textBlock" Text="{Binding Path=Tests[0].A}"
                   HorizontalAlignment="Left" Margin="252,28,0,0" TextWrapping="Wrap"
                   VerticalAlignment="Top"/>
    </Grid>
</Page>

代碼隱藏文件

using System.Windows;
using System.Windows.Controls;

namespace GestionStockWPF
{
    public partial class MainPage : Page
    {
        public MainPageViewModel ViewModel;
        public MainPage()
        {
            InitializeComponent();
            ViewModel = new MainPageViewModel();

            // If I do DataContext = this;
            // and then set the binding of the TextBlock to "ViewModel.Tests[0].A" nothing is shown
            DataContext = ViewModel;

            // If I don't do that the listview does not contain the correct number of rows
            listView.ItemsSource = ViewModel.tests;
        }
    }
}

查看模型和測試 class

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace GestionStockWPF
{
    // Not sure if I have to implement INotifyPropertyChanged but my real class does it
    public class Test : INotifyPropertyChanged
    {
        public string A { get { return "A"; } }
        public string B { get { return "B"; } }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
    public class MainPageViewModel
    {
        public ObservableCollection<Test> Tests { get; set; }
        public MainPageViewModel()
        {
            tests = new ObservableCollection<Test>();
            for (int i = 0; i < 50; ++i)
            {
                Tests.Add(new Test());
            }
        }
    }
}

您的綁定不正確。

它應該是:

ItemsSource="{Binding tests}"

您需要在綁定中指定路徑,而不是綁定源。

顯式Path部分在綁定中是可選的,因此您還可以將文本塊綁定更改為:

Text="{Binding tests[0].A}"

順便說一句,屬性應該使用PascalCase ,所以最好稱它為Tests

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM