开发者

GUI/view doesn't notice about changes in the viewmodel. Who should notify?

开发者 https://www.devze.com 2023-04-07 23:36 出处:网络
I\'m rather new to Silverlight and have a question about the notifying-mechanism. My solution is an MVVM-application stacked like this:

I'm rather new to Silverlight and have a question about the notifying-mechanism. My solution is an MVVM-application stacked like this:

VIEW Contains a RadGridView bound to a collection in the viewmodel, the data is an entitycollection. The GridView's SelectedItem is bound to corresponding property in the viewmodel.

VIEWMODEL Holds the properties below that the GridView is bound to and implements INotifyPropertyChanged. •SelectList - an entitycollection that inherits ObservableCollection. When SelectList is set, it runs a notify-call. •SelectedItem - an entity that also implements INotifyPropertyChanged for its own properties. When SelectedItem is set, it runs a notify-call.

My question is, who should make the notify-call so that the GridView knows value has changed? Occasionally, a property in the entity is set programmatically directly in the viewmodel. As for now, nothing is happening in the GUI although the properties gets the new values correctly.

Regards, Clas

-- UPDATE WITH CODE -------------------------

VIEW

<UserControl 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    x:Class="X.Y.Z.MonthReport.MonthReportView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot">
        <telerik:RadGridView x:Name="MonthReportGrid"
                             Grid.Row="1"
                             ItemsSource="{Binding SelectList}"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                             AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <!-- The other columns have been cut out of this example -->
                <telerik:GridViewDataColumn DataMemberBinding="{Binding curDate, Mode=TwoWay, TargetNullValue=''}" DataFormatString="{} {0:d}" Header="Avläst datum" UniqueName="curDate" IsVisible="True" IsReadOnly="False">
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate>
                            <telerik:RadDateTimePicker SelectedValue="{Binding curDate, Mode=TwoWay, TargetNullValue=''}" InputMode="DatePicker" DateTime开发者_开发百科WatermarkContent="ÅÅÅÅ-MM-DD" />
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding curValue, Mode=TwoWay, TargetNullValue=''}" Header="Avläst värde" UniqueName="curValue" IsVisible="True" IsReadOnly="False" />
        </telerik:RadGridView>
    </Grid>
</UserControl>

VIEW .CS

using System;
using System.Collections.Generic;
using System.Windows.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;


namespace X.Y.Z.MonthReport
{

    public partial class MonthReportView : UserControl, IMonthReportView
    {
        /// <summary>
        /// ViewModel attached to the View
        /// </summary>
        public IMonthReportViewModel Model
        {
            get {   return this.DataContext as IMonthReportViewModel; }
            set {   this.DataContext = value; }
        }

        public MonthReportView()
        {
            InitializeComponent();
            this.MonthReportGrid.CellEditEnded += new EventHandler<GridViewCellEditEndedEventArgs>(MonthReportGrid_OnCellEditEnded);
        }


        public void MonthReportGrid_OnCellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
        {
            if (e.Cell.Column.UniqueName == "curValue")
            {
                // ...
                this.Model.SetAutomaticReadingDate();
            }

            if (e.Cell.Column.UniqueName == "curDate")
            {
                this.Model.UpdateAutomaticReadingDate();
            }
        }
    }
}

VIEWMODEL

using System;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Commands;


namespace X.Y.Z.MonthReport
{
    public class MonthReportViewModel : ViewModel<IMonthReportView>, IMonthReportViewModel
    {
        private readonly IEventAggregator eventAggregator;
        private readonly IMonthReportService dataService;
        private readonly IMonthReportController dataController;


        private DateTime? _newReadingDate;
        public DateTime? NewReadingDate
        {
            get { return _newReadingDate; }
            set { _newReadingDate = value; }
        }

        //Holds the selected entity
        private MonthReportEntity _selectedItem;
        public MonthReportEntity SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    //The INotifyPropertyChanged implementation inherited from ViewModel-base.
                    Notify(() => this.SelectedItem);
                }
            }
        }

        //The entitycollection
        private MonthReportEntityCollection _selectList;
        public MonthReportEntityCollection SelectList
        {
            get { return _selectList; }
            set
            {
                if (_selectList != value)
                {
                    _selectList = value;
                    //The INotifyPropertyChanged implementation inherited from ViewModel-base.
                    Notify(() => this.SelectList);
                }
            }
        }

        public MonthReportViewModel(IMonthReportView view,
            IEventAggregator eventAggregator, IMonthReportService dataService, IMonthReportController dataController)
        {
            this.InitializeCommands();
            this.eventAggregator = eventAggregator;
            this.dataController = dataController;
            this.dataService = dataService;
            this.View = view;
            this.View.Model = this;

            dataService.onGetMonthReportComplete += new EventHandler<MonthReportEventArgs>(OnGetMonthReportComplete);
            dataService.onSaveMonthReportComplete += new EventHandler<MonthReportEventArgs>(OnSaveMonthReportComplete);

            InitializeData();
        }

        public void InitializeCommands()
        {
            // ...
        }

        public void InitializeData()
        {
            GetMonthReport();
        }

        //This function is not working as I want it to.
        //The gridview doesn't notice the new value.
        //If a user edits the grid row, he should not need to
        //add the date manually, Therefor I use this code snippet.
        public void SetAutomaticReadingDate()
        {
            if ((NewReadingDate.HasValue) && (!SelectedItem.curDate.HasValue))
            {
                SelectedItem.curDate = NewReadingDate;
                //The INotifyPropertyChanged implementation inherited from ViewModel-base.
                Notify(() => this.SelectedItem.curDate);
            }
        }

        public void GetMonthReport()
        {
            dataService.GetMonthReport();
        }

        public void SaveMonthReport()
        {
            dataService.SaveMonthReport(SelectList);            
        }

        void OnGetMonthReportComplete(object sender, MonthReportEventArgs e)
        {
            // ...
        }

        void OnSaveMonthReportComplete(object sender, MonthReportEventArgs e)
        {
            // ...       
        }

        #region ICleanable
        public override void Clean()
        {
            base.Clean();
        }
        #endregion
    }
}


if you do your binding like this

<telerik:GridViewDataColumn DataMemberBinding="{Binding curValue, Mode=TwoWay, TargetNullValue=''}" Header="Avläst värde" UniqueName="curValue" IsVisible="True" IsReadOnly="False" />

you just have to look at the binding to know where you have to call PropertyChanged and your binding said:

the class whith the property "curValue" has to implement INotifyProperyChanged to get the View informed.

  public void SetAutomaticReadingDate()
    {
        if ((NewReadingDate.HasValue) && (!SelectedItem.curDate.HasValue))
        {
            //this is enough if the class of SelectedItem implements INotifyPropertyChanged
            //and the curDate Poperty raise the event 
            SelectedItem.curDate = NewReadingDate;               
        }
    }

btw BAD code style to name the Property curDate! it should be CurDate, Properties with camlCase hurts my eyes :)


Your "MonthReportEntityCollection" must implement interface "INotifyCollectionChanged" to allow informing UI about collection changes (items add/remove). Your "MonthReportEntity" must implement interface "INotifyPropertyChanged" to allow informing UI about entitie's property changing. Other stuff looks correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号