DataBind到Combobox在SilverLight 4 .0

时间:2013-04-17 04:08:33

标签: silverlight-4.0 silverlight-5.0

我正在尝试数据绑定到组合框。数据来自数据库表,其名称为tbltest,表有2个fileds id和name。

当我尝试将名称绑定到combox时,它会在View中显示tbltest:name。我正在使用域服务和MVVM绑定数据。

以下是我的ViewModel代码:

   public ViewModel()
    {
        var query = context.GetTblTestsQuery();
        var load = context.Load(query);
        load.Completed += (s, ea) =>
        {
            ObsCompanyCollection = new ObservableCollection<tblTest>(context.tblTests);

        };

    }
  private ObservableCollection<tblTest> _ObsCompanyCollection = new ObservableCollection<tblTest>();     
    public ObservableCollection<tblTest> ObsCompanyCollection
    {
        get
        {
            return _ObsCompanyCollection;
        }
        set
        {
            if (_ObsCompanyCollection != value)
            {
                _ObsCompanyCollection = value;
                NotifyPropertyChanged("ObsCompanyCollection");
            }
        }

    }

以下是我的XAml文件的代码:

 <UserControl.Resources>
    <my:ViewModel x:Key="ViewModel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource ViewModel}">


    <ComboBox Height="23" HorizontalAlignment="Left" Margin="47,128,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DisplayMemberPath="{Binding name,Mode=TwoWay}" ItemsSource="{Binding ObsCompanyCollection,Mode=TwoWay}" SelectedItem="{Binding tbldata.SelectCompanyId,Mode=TwoWay}" />

我不知道这段代码有什么问题。我只想在我的组合框中显示名称。

谢谢

1 个答案:

答案 0 :(得分:1)

试试这个                        

    <ComboBox Height="23" HorizontalAlignment="Left" Margin="47,128,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DisplayMemberPath="name" ItemsSource="{Binding ObsCompanyCollection,Mode=OneWay}"