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>
この現象の理由は何ですか?
My DataGridはList
AddressListのインスタンスにバインドされています。クラスAddressにはCountryというプロパティがあります。 DataGridComboBoxColumnはこのプロパティにバインドされ、ItemsSourceのList他のコンボボックスが機能します! – PeRa
私はあなたのコードを見てきました:) そして私はあなたのDataGridComboBoxがうまくいかない理由を説明しようとしました。これは、正しいDataContextにないためです。各行にはDataContextとしてのAddressオブジェクトがあり、AddressオブジェクトにはCountryListはありません。そういうわけではないのです。 DataGrid外のComboBoxには、別のDataContextがあります。私のxamlコードを試しましたか? –
ありがとうございます。あなたのxamlは動作し、私はあなたの説明を理解します。かなり複雑です。つまり、ItemsSourceプロパティを使用したいのであれば、私はStaticResourceで作業する必要がありますか? – PeRa