0

コンボボックスにデータバインドしようとしています。データは、tbltest という名前のデータベース テーブルから取得され、テーブルには ID と名前の 2 つのフィールドがあります。

名前をコンボックスにバインドしようとすると、ビューに 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}" />

このコードの何が問題なのかわかりません。コンボボックスに名前だけを表示したい。

ありがとう

4

1 に答える 1

1

これを試して

    <ComboBox Height="23" HorizontalAlignment="Left" Margin="47,128,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DisplayMemberPath="name" ItemsSource="{Binding ObsCompanyCollection,Mode=OneWay}"
于 2013-04-24T06:34:55.807 に答える