我有一个带有域服务的简单项目,我正在尝试将组合框与我的 View 模型中的域服务绑定(bind)。
我正在使用 mvvm
设计模式,注意当我不使用 mvvm
设计模式,我正在从后面的代码中绑定(bind)组合框,我在组合框上看到了结果。
enter code here
public class MainViewModel:INotifyPropertyChanged
{
DomainService1 ctx = new DomainService1();
private ObservableCollection<product> _products;
public ObservableCollection<product> Products
{
get { return _products; }
set {
if (value != _products)
{
_products = value;
OnPropertyChanged("Products");
}
}
}
public MainViewModel()
{
if (!DesignerProperties.IsInDesignTool)
{
LoadProdcuts();
}
}
private void LoadProdcuts()
{
ctx.Load(ctx.GetProductsQuery(), LoadProdcutCallBack, null);
}
private void LoadProdcutCallBack(LoadOperation<product>lo)
{
_products = new ObservableCollection<product>(lo.Entities);
}
private void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
enter code here
<UserControl.Resources>
<data:MainViewModel x:Key="VwModel"/>
</UserControl.Resources>
<ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="100" ItemsSource="{Binding Products}"/>
最佳答案
您必须设置 DataContext
属性(property):
<ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="100"
DataContext="{StaticResource VwModel}"
ItemsSource="{Binding Products}"/>
关于c# - 无法将简单的组合框与 mvvm 和 wcf ria 服务绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551785/