C# MVVM灯光绑定到可观察集合
我将MVVM light与EF4和SQL CE 4结合使用,但我的可观察集合存在问题。我的应用程序不一定需要使用mvvm模式,但由于我需要observablecollection的好处,我决定学习如何集成它。我可以成功地将我的属性实体数据库链接到我的列表框并显示它们,我还可以将这些实体的某些属性链接到文本框,但我遇到的问题是当我试图通过在文本框中键入来更新这些属性时。以下是我的文本框和列表框的xaml代码:C# MVVM灯光绑定到可观察集合,c#,wpf,entity-framework,mvvm,C#,Wpf,Entity Framework,Mvvm,我将MVVM light与EF4和SQL CE 4结合使用,但我的可观察集合存在问题。我的应用程序不一定需要使用mvvm模式,但由于我需要observablecollection的好处,我决定学习如何集成它。我可以成功地将我的属性实体数据库链接到我的列表框并显示它们,我还可以将这些实体的某些属性链接到文本框,但我遇到的问题是当我试图通过在文本框中键入来更新这些属性时。以下是我的文本框和列表框的xaml代码: <TextBox Text="{Binding SaleTitle, Valid
<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>
Load());
返回loadCommand;
}
}
专用空心荷载()
{
PropertyList=新的ObservableCollection((来自entities.Properties.Include(“图像”)中的属性)
选择属性);
propertyView=CollectionViewSource.GetDefaultView(PropertyList);
如果(propertyView!=null)
propertyView.CurrentChanged+=新系统.EventHandler(propertyView\u CurrentChanged);
RaisePropertyChanged(“当前联系人”);
RaiseProperty变更(“出售所有权”);
RaiseProperty变更(“地址”);
RaiseProperty变更(“拍卖商名称”);
RaisePropertyChanged(“代理名称”);
提高财产变动(“价格”);
RaisePropertyChanged(“NextBid”);
RaisePropertyChanged(“状态”);
}
void propertyView\u CurrentChanged(对象发送方,System.EventArgs e)
{
RaisePropertyChanged(“当前联系人”);
RaiseProperty变更(“出售所有权”);
RaiseProperty变更(“地址”);
RaiseProperty变更(“拍卖商名称”);
RaisePropertyChanged(“代理名称”);
提高财产变动(“价格”);
RaisePropertyChanged(“NextBid”);
RaisePropertyChanged(“状态”);
}
私有财产;
公共财产
{
得到
{
如果(propertyView!=null)
返回propertyView.CurrentItem作为属性;
返回null;
}
设置
{
_CurrentProperty=值;
RaisePropertyChanged(“CurrentProperty”);
}
}
公共可观测集合属性列表
{
得到
{
返回属性列表;
}
设置
{
如果(propertyList==值)
{
返回;
}
var oldValue=propertyList;
propertyList=值;
//更新绑定,无广播
RaisePropertyChanged(PropertiesPropertyName);
}
}
公共主视图模型()
{
如果(IsInDesignMode)
{
//代码在混合-->创建设计时数据中运行。
}
其他的
{
//代码“真实地”运行
实体=新模型1容器1();
}
}
////公共覆盖无效清除()
////{
//////如果需要,请清理
////base.Cleanup();
////}
}
}
列表框成功地填充了当前选定项目的内容,但当我输入并单击退出列表框或执行任何操作以失去焦点时,列表框只会返回到以前的内容。查看您的SaletTitle属性定义。它读取CurrentProperty.Saletitle中的值,但将值设置为本地字段,而该字段在任何地方都不使用。我发誓我已经尝试过了,真不敢相信它这么简单-一定是我连续10个小时后在玩把戏!
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();
////}
}