別のItemsSourceにバインドするDataGridTemplateColumnコンボボックスがあります。このコンボボックスを別のソースにバインドしてDataContextを変更すると思われます。WPFバインドされたDataGridコンボボックス選択されたDataGridRowのデータコンテキストに
私は、DataGrid内の選択された行のDataContextにコンボボックス内の選択項目の値をバインドする際に問題が発生しています。
XAML:
<DataGrid HorizontalAlignment="Left" ItemsSource="{Binding Path=WorldDataList}" SelectedItem="{Binding SelectedWorldData}">
<DataGridTemplateColumn Header="Country" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}},Path=DataContext.Countries}"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=SelectedItem.Country}"
SelectedIndex="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
のC#:
class WorldDataViewModel : ObservableObject
{
private ObservableCollection<WorldData> _worldDataList = new ObservableCollection<WorldData>();
public ObservableCollection<WorldData> WorldDataList
{
get { return _worldDataList; }
set
{
Set(ref _worldDataList, value);
}
}
public List<string> Countries {get;set;}
private WorldData worldData;
private WorldData SelectedWorldData
{
get{return worldData;}
set
{
Set(ref worldData, value);
}
}
}
class WorldData : ObservableObject
{
private string country;
public string Country
{
get{return country;}
set
{
Set(ref country, value);
}
}
私は例外を取得:
System.Windows.Data Error: 23 : Cannot convert '{NewItemPlaceholder}' from type 'NamedObject' to type 'WorldData' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter cannot convert from MS.Internal.NamedObject. at System.ComponentModel.TypeConverter.GetConvertFromException(Object value) at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)' Exception thrown: 'System.NotSupportedException' in PresentationFramework.dll
私は型コンバータを使用する必要があるように思えるが、それは私がいけないようです私が見つけることができたほんの少しからこれを行う
私は今、私はあきらめて、コンボボックス用の別のボックスを追加し、Datagridの選択項目にバインドするつもりだと思います。私は直感的なものではありませんので、賢いアイデアがあればそれを見つけてください。
こんにちは。ちょうどこれを見て。 SelectedCountryにバインドできましたか? SelectedWorldData.Countryにブレークポイントを設定して、国を選択したときに実際にヒットするかどうかを確認しましたか? また、CanUserAddRowsをtrueにする必要があります。あなたの試みをありがとう –
申し訳ありませんが、私は最初の答えを更新しました。今度は 'Country'にブレークを設定すると、デバッガがそこで停止します。 – gomi42
'CanUserAddRows'をtrueに設定できることを忘れてしまいました。新しく作成された行には、UXの観点から少し混乱するかもしれないComboboxが表示されるので、私はそれを無効にしました。再び技術的な制限はありません。 – gomi42