绑定不使用从另一个任务分配的属性

时间:2015-11-02 14:57:11

标签: wpf asynchronous binding

有人可以快速告诉我为什么绑定到VesselCollection的DataGrid什么都没显示。 ReferentialVesselViewModel类是ReferentialVesselView视图的DataContext(在XAML下面)。 DataGrid的ItemsSource是VesselCollection,它是一个ObservableCollection。

 public class ReferentialVesselViewModel : ViewModelBase
    {
        public ReferentialVesselViewModel()
        {
           Task.Factory.StartNew(() =>
                    App.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {

                        VesselEntities = ServiceProxy.Load<VesselProxy, IEnumerable<Vessel>>(proxy => proxy.GetVesselTable());

                        foreach (var vessel in VesselEntities)
                            VesselCollection.Add(new ReferentialVesselLineViewModel(vessel));
                    })));
        }

        public ObservableCollection<ReferentialVesselLineViewModel> VesselCollection { get; private set; }
    }

Xaml这边是代码

<UserControl  
    x:Class="MyCompany.Referential.ReferentialVesselView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:ViewModels="clr-namespace:MyCompany.ViewModel"
    >

    <UserControl.DataContext>
        <ViewModels:ReferentialVesselViewModel/>
    </UserControl.DataContext>

    <Border BorderThickness="0.5" BorderBrush="SteelBlue" Margin="5" Padding="10"  Background="White">
        <DataGrid 
                ItemsSource="{Binding VesselCollection, Mode=OneWay}" 
                AutoGenerateColumns="False" 
                CanUserAddRows="False" 
                CanUserResizeColumns="False" 
                IsReadOnly="True">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Vessel Code" Binding="{Binding VesselCode}" />
                    <DataGridTextColumn Header="Vessel Name" Binding="{Binding VesselName}" />
                </DataGrid.Columns>
        </DataGrid>
    </Border>

</UserControl>

最后这是暴露VesslName,VesselCode等属性的类。

public class ReferentialVesselLineViewModel : ViewModelBase
    {
        private Vessel vessel;

        public ReferentialVesselLineViewModel(Vessel vessel)
        {
            this.vessel = vessel;
        }

        public String VesselName
        {
            get { return vessel.Name; }
            set
            {
                if(vessel.Name != value)
                {
                    vessel.Name = value;
                    RaisePropertyChanged("VesselName");
                }
            }
        }

        public String VesselCode
        {
            get { return vessel.Code; }
            set
            {
                if(vessel.Code != value)
                {
                    vessel.Code = value;
                    RaisePropertyChanged("VesselCode");
                }
            }
        }

        //...etc

    }

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,事实上,我应该在收集集合后立即引发NotifyPropertyChanged事件。

    public class ReferentialVesselViewModel : ViewModelBase
        {
            public ReferentialVesselViewModel()
            {
               Task.Factory.StartNew(() =>
                        App.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {

                            VesselEntities = ServiceProxy.Load<VesselProxy, IEnumerable<Vessel>>(proxy => proxy.GetVesselTable());

                            foreach (var vessel in VesselEntities)
                                VesselCollection.Add(new ReferentialVesselLineViewModel(vessel));
 RaisePropertyChanged(() => VesselCollection);
                        })));
            }

                public ObservableCollection<ReferentialVesselLineViewModel> VesselCollection { get; private set; }
            }