2017-03-23 17 views
1

My DataGridComboBoxColumnにデータが表示されません。空っぽです。 ComboBoxには問題はありませんが、DataGridComboBoxColumnは機能しません。
.NetFramework 4.6.1DataGridComboBoxColumnが空です。

モデル:

public class Address 
{ 
    public string Firstname { get; set; } 
    public string Lastname { get; set; } 
    public string Country { get; set; } 
} 

のViewModel:

public class AddressViewModel 
{ 
    public AddressViewModel() 
    { 
     LoadAdresses(); 
     LoadCountries(); 
    } 

    public List<Address> AddressList { get; set; } 
    public List<string> CountryList { get; set; } 


    private void LoadAdresses() 
    { 
     AddressList = new List<Model.Address>(); 
     AddressList.Add(new Model.Address(){ Firstname = "Peter", Lastname = "R.", Country = "A" }); 
     AddressList.Add(new Model.Address(){ Firstname = "Tom", Lastname = "A.", Country = "A" }); 
     AddressList.Add(new Model.Address(){ Firstname = "Sam", Lastname = "F.", Country = "A" }); 
    } 

    private void LoadCountries() 
    { 
     CountryList = new List<string>(); 
     CountryList.Add("A"); 
     CountryList.Add("D"); 
     CountryList.Add("CH"); 
     CountryList.Add("GB"); 
     CountryList.Add("F"); 
    } 
} 

ビュー:

<Window.DataContext> 
    <vm:AddressViewModel/> 
</Window.DataContext> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <DataGrid x:Name="AddressDataGrid" Grid.Row="0" ItemsSource="{Binding AddressList}" AutoGenerateColumns="False" > 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Firstname" Binding="{Binding Firstname}" />    
      <DataGridTextColumn Header="Lastname" Binding="{Binding Lastname}" /> 
      <DataGridComboBoxColumn Header="Country" 
         SelectedItemBinding="{Binding Country}" 
         ItemsSource="{Binding CountryList}"/> 
     </DataGrid.Columns> 
    </DataGrid> 

    <!--This ComboBox works--> 
    <ComboBox Grid.Row="1" ItemsSource="{Binding CountryList}"/> 
</Grid> 

この現象の理由は何ですか?

答えて

1

DataGridComboBoxColumnのプロパティCountryListにバインドしています。しかし、このプロパティはあなたのクラスにありませんAddress;あなたのクラスにありますAddressViewModel。それであなたの中に何も見えないのはなぜですか?ComboBox;バインドは有効ではありません。

あなたDataGrid 1ライン分のあなたのList<Address> AddressListスタンドの各エントリに、あなたは「通常」は{Binding Property}を結合して各行にAddressの各プロパティにアクセスすることができます。しかし、List<Address> AddressListを保持するクラスにあるプロパティにアクセスしたいとします。そのため、バインドには別の方法を使用する必要があります。二つの方法が考えられますElementName

経由

  • 結合を結合

    • RelativeSourceは、これはあなた

      <DataGridComboBoxColumn Header="Country" 
               SelectedItemBinding="{Binding Country}"> 
          <DataGridComboBoxColumn.ElementStyle> 
           <Style TargetType="{x:Type ComboBox}"> 
            <Setter Property="ItemsSource" 
              Value="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
           </Style> 
          </DataGridComboBoxColumn.ElementStyle> 
          <DataGridComboBoxColumn.EditingElementStyle> 
           <Style TargetType="{x:Type ComboBox}"> 
            <Setter Property="ItemsSource" 
              Value="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /> 
           </Style> 
          </DataGridComboBoxColumn.EditingElementStyle> 
      </DataGridComboBoxColumn> 
      

      そして、ちょうどヒントのために働く必要があります:あなたはあなたのビューの使用にコレクションをバインドする場合表示を最新の状態に保つためにListの代わりにObservableCollectionを追加してください。

  • +0

    My DataGridはList

    AddressListのインスタンスにバインドされています。クラスAddressにはCountryというプロパティがあります。 DataGridComboBoxColumnはこのプロパティにバインドされ、ItemsSourceのList CountryListのインスタンスを使用してコンボボックスコントロールのコンテンツを生成します。
    他のコンボボックスが機能します! – PeRa

    +0

    私はあなたのコードを見てきました:) そして私はあなたのDataGridComboBoxがうまくいかない理由を説明しようとしました。これは、正しいDataContextにないためです。各行にはDataContextとしてのAddressオブジェクトがあり、AddressオブジェクトにはCountryListはありません。そういうわけではないのです。 DataGrid外のComboBoxには、別のDataContextがあります。私のxamlコードを試しましたか? –

    +0

    ありがとうございます。あなたのxamlは動作し、私はあなたの説明を理解します。かなり複雑です。つまり、ItemsSourceプロパティを使用したいのであれば、私はStaticResourceで作業する必要がありますか? – PeRa

    関連する問題