【问题标题】:How to bind an Observable Collection to a ListView如何将 Observable Collection 绑定到 ListView
【发布时间】:2015-05-23 21:20:11
【问题描述】:

我已经按照这个示例设置了 ListView 的绑定 binding to observable collection,但是当我运行应用程序时,集合值不会显示在 ListView 中。

输出窗口没有抛出任何绑定错误,因此不确定绑定错误可能是什么。 此外,我在列表上设置了一个断点,然后将其发送到第二个 VM 并填充它,即不为空。

我的猜测是第二个虚拟机中的列表为空,因为它在被传递后没有正确初始化。

谁能建议如何调试 ListView 为空?

这是视图中设置的绑定:

<ListBox ItemsSource="{Binding AddedSubjectGradePairsCopy}" Height="400" Margin="0,0,0,-329" VerticalAlignment="Top">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                        <Run Text="{Binding Subject}" /><Run Text=" - " /><Run Text="{Binding Points}" />
                        </TextBlock>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

View的数据上下文在后面的代码中设置如下:

namespace LC_Points.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ViewSubjectGradePage : Page
    {
        private NavigationHelper navigationHelper;

        private ViewSubjectGradeViewModel ViewModel;

        public ViewSubjectGradePage()
        {
            this.InitializeComponent();

            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
            this.navigationHelper.SaveState += this.NavigationHelper_SaveState;

            ViewModel = new ViewSubjectGradeViewModel();
            this.DataContext = ViewModel;
        }

        /// <summary>
        /// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }


        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param>
        /// <param name="e">Event data that provides an empty dictionary to be populated with
        /// serializable state.</param>
        private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
        }

        #region NavigationHelper registration

        /// <summary>
        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// <para>
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="NavigationHelper.LoadState"/>
        /// and <see cref="NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.
        /// </para>
        /// </summary>
        /// <param name="e">Provides data for navigation methods and event
        /// handlers that cannot cancel the navigation request.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedFrom(e);
        }

        #endregion
    }
}

并通过构造函数在 ViewSubjectGradeVM 中接收列表:

namespace LC_Points.ViewModel
{
    public class ViewSubjectGradeViewModel 
    {


        public ViewSubjectGradeViewModel()
        {


        }

         /// <summary>
        /// Initializes a new instance of the ViewSubjectGradeViewModel class.
        /// </summary>
        public ViewSubjectGradeViewModel(IEnumerable<ScoreModel> addedSubjectGradePairs)
        {
            this.AddedSubjectGradePairsCopy = addedSubjectGradePairs;

        }


        //Property for collection passed from MainViewModel
        public IEnumerable<ScoreModel> AddedSubjectGradePairsCopy { get; set; }


    }
}

这是从 MainVM 传递到 ViewSubjectGradeVM 的 List 的支持模型:

namespace LC_Points.Model
{
    public class ScoreModel : INotifyPropertyChanged
    {

        // The name of the subject.
        public string Subject { get; set; }


        // The points paired with each grade type.
        public int Points { get; set; }


        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    }
}

【问题讨论】:

  • 你在哪里设置DataContext
  • 我已经在视图后面的代码中设置了数据上下文,现在将发布一个编辑。
  • 是的,但是你调用了默认的ctor所以AddedSubjectGradePairsCopy是空的。
  • 好的,我现在明白了,那我只需要摆脱它吗? hastebin.com/oretezoqot.cs
  • 您不应该删除默认 ctor。你需要类似ViewModel = new ViewSubjectGradeViewModel(GetScoreModels()); this.DataContext = ViewModel; 而不是ViewModel = new ViewSubjectGradeViewModel(); this.DataContext = ViewModel;

标签: c# listview mvvm observablecollection win-universal-app


【解决方案1】:

试试这样的。当然,你的模型应该来自一些你应该映射到你的视图模型的源。但这向您展示了设置正确数据上下文的方法。

var scoremodels = new List<ScoreModel>
{
    new ScoreModel {Subject = "Subj1", Points = 6},
    new ScoreModel {Subject = "Subj2", Points = 3},
    new ScoreModel {Subject = "Subj3", Points = 8},
}
ViewModel = new ViewSubjectGradeViewModel(scoreModels);
this.DataContext = ViewModel;

【讨论】:

  • 好吧,如果我理解正确的话,我应该定义从 MainVM 传递到这个 VM 的“事物”。但是当我在后面的 ViewSubjectGradePage 代码中尝试以下操作时,编译器告诉我该列表在当前上下文中不存在。 ` ViewModel = new ViewSubjectGradeViewModel(AddedSubjectGradePairs); ` AdditionalSubjectGradePairs 是从由 ScoreModel 组成的 MainVM 传递的列表。
  • 你尝试过什么?显示 ViewSubjectGradePage 代码。
  • 这是我尝试过的,但编译器告诉我该列表不存在当前上下文,这是真的,因为该列表在我的 MainVM 中。该列表是一个 ScoreModel 类型的 Observable Collection,它是我的数据模型。我的 ViewSubjectGradePage 代码:gist.github.com/BrianJVarley/87a051d400ea1c7ffcfb,这是我在 viewGradesCommand RelayCommand 中将列表传递给该页面的 VM 的主 VM:gist.github.com/BrianJVarley/3dd6e164029aeb67263d
  • 我没有看到你在哪里创建了MainViewModel。此外,AddedSubjectGradePairs 也无法从 ViewSubjectGradePage 访问。
  • 这是创建列表并将其传递给其他 VM 的 MainViewModel:hastebin.com/alirebanup.cs
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 2016-04-17
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多