c# - 无法将简单的组合框与 mvvm 和 wcf ria 服务绑定(bind)

标签 c# silverlight mvvm wcf-ria-services

我有一个带有域服务的简单项目,我正在尝试将组合框与我的 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/

相关文章:

c# - WCF 并将代理代码移动到 DLL。是否可以?

c# - ASP.NET Core Web API - 如何解决 Serilog 和 Microsoft.Extensions.Logging 之间的冲突

c# - 当用户开始输入 wpf 时启用按钮

silverlight - 如何从网络浏览器打开应用程序 Windows Phone

c# - 有什么方法可以避免实现 ICommand 以在 MVVM 中使用 Button?

c# - 如果不重新启动,我无法在 Datagridview 中看到新元素

Silverlight AutoCompleteBox NotifyPropertyChanged 不更新屏幕

c# - WP7动态更新UI而不阻塞线程

wpf - DataContext 用于在 DataTemplate 中将 UserControl 与 ViewModel 绑定(bind)

c# - 代码契约和 MVVM