c# - MVVM Light 绑定(bind)到 Observable 集合

标签 c# wpf entity-framework mvvm

我将 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 Light 绑定(bind)到 Observable 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10169613/

相关文章:

linq-to-sql - 微软试图用所有这些数据访问策略解决什么问题?

.net - Entity Framework 4.1:如何按对象ID删除

c# - 如果未找到动态内容,则将用户正确发送到 404 (ASP.NET MVC)

c# - 如何将字符串从 C# 传递到 javascript 变量

c# - 检查和检索集合中第一项的最佳方法是什么?

c# - 如何将非英文命名文件添加到 Visual Studio 2010 中的安装项目?

c# - WPF MVVM 案例 : ItemsControl contains hyperlink and command to update property in ViewModel

c# - Wpf 渐变等效于 css 渐变

c# - 如何为鼠标创建一个控件 "transparent"或将MouseMove事件路由到父级?

wcf - 是否建议将 self 跟踪实体与 WCF 服务一起使用?