我将MVVM light与EF4和SQL CE 4结合使用,但是我的可观察集合有问题。我的应用程序不一定需要使用mvvm模式,但是由于我需要observablecollection的好处,因此我决定学习如何集成它。我可以将属性实体的数据库成功链接到列表框并显示它们,也可以将这些实体的某些属性链接到文本框,但是当我尝试通过在文本框中键入内容来更新这些属性时,我陷入了困境。这是我的文本框和列表框的xaml代码:

 <TextBox Text="{Binding SaleTitle, ValidatesOnDataErrors=true, Mode=TwoWay}"
  <ListBox Height="424"
        Margin="24,80,0,0"
        x:Name="listBoxProperties"
        VerticalAlignment="Top"
        ItemTemplate="{StaticResource propertySummaryTemplate}"
        IsSynchronizedWithCurrentItem="True"
        Width="216" BorderThickness="0" Background="{x:Null}"
        FontFamily="Segoe UI"
        ItemsSource="{Binding PropertyList}"
        SelectedItem="{Binding CurrentProperty, Mode=TwoWay}"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        UseLayoutRounding="True"
        HorizontalAlignment="Left"
        ScrollViewer.VerticalScrollBarVisibility="Disabled" >
    </ListBox>

这是我的MainViewModel的一部分代码:
   private string _SaleTitle;
    public string SaleTitle
    {
        get
        {
            if (CurrentProperty != null)
                return CurrentProperty.SaleTitle;
            else
                return "";
        }
        set
        {
            _SaleTitle = value;
            RaisePropertyChanged("SaleTitle");
        }
    }



              private RelayCommand loadCommand;
    public ICommand LoadCommand
    {
        get
        {
            if (loadCommand == null)
                loadCommand = new RelayCommand(() => Load());
            return loadCommand;
        }
    }
    private void Load()
    {
        PropertyList = new ObservableCollection<Property>((from property in entities.Properties.Include("Images")
                                                          select property));
        propertyView = CollectionViewSource.GetDefaultView(PropertyList);
        if (propertyView != null)
            propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged);
        RaisePropertyChanged("CurrentContact");
        RaisePropertyChanged("SaleTitle");
        RaisePropertyChanged("Address");
        RaisePropertyChanged("AuctioneerName");
        RaisePropertyChanged("AgentName");
        RaisePropertyChanged("Price");
        RaisePropertyChanged("NextBid");
        RaisePropertyChanged("Status");
    }


        void propertyView_CurrentChanged(object sender, System.EventArgs e)
    {
        RaisePropertyChanged("CurrentContact");
        RaisePropertyChanged("SaleTitle");
        RaisePropertyChanged("Address");
        RaisePropertyChanged("AuctioneerName");
        RaisePropertyChanged("AgentName");
        RaisePropertyChanged("Price");
        RaisePropertyChanged("NextBid");
        RaisePropertyChanged("Status");
    }

    private Property _CurrentProperty;
    public Property CurrentProperty
    {
        get
        {
            if (propertyView != null)
                return propertyView.CurrentItem as Property;
            return null;
        }

        set
        {
            _CurrentProperty = value;
            RaisePropertyChanged("CurrentProperty");
        }
    }


     public ObservableCollection<Property> PropertyList
    {
        get
        {
            return propertyList;
        }

        set
        {
            if (propertyList == value)
            {
                return;
            }

            var oldValue = propertyList;
            propertyList = value;

            // Update bindings, no broadcast
            RaisePropertyChanged(PropertiesPropertyName);
        }
    }

    public MainViewModel()
    {
        if (IsInDesignMode)
        {
            // Code runs in Blend --> create design time data.
        }
        else
        {
            // Code runs "for real"
            entities = new Model1Container1();
        }
    }

    ////public override void Cleanup()
    ////{
    ////    // Clean up if needed

    ////    base.Cleanup();
    ////}
}

}

列表框已成功填充了当前所选项目的内容,但是当我键入该列表框并单击该列表框或单击以进行失去焦点的任何操作时,它仅返回到之前的内容。

最佳答案

看一下您的SaleTitle属性定义。它从CurrentProperty.Saletitle读取值,但将值设置为本地字段,该字段不再使用。

关于c# - MVVM灯光绑定(bind)到可观察的集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10169613/

10-14 16:47