2016-12-23 52 views
1

別の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の選択項目にバインドするつもりだと思います。私は直感的なものではありませんので、賢いアイデアがあればそれを見つけてください。

答えて

1

ここでは、選択した国をWorldDataビューモデルに戻す完全なテストアプリケーションです。 がSelectedItemにないと動作しません。

<Window 
    x:Class="ComboboxInDataGrid.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Title="MainWindow" 
    Width="525" 
    Height="350" 
    mc:Ignorable="d"> 
    <Grid> 
     <DataGrid x:Name="me" 
      HorizontalAlignment="Left" 
      AutoGenerateColumns="False" 
      CanUserAddRows="False" 
      ItemsSource="{Binding Path=WorldDataList}" 
      SelectedItem="{Binding SelectedWorldData}"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Country"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Grid> 
           <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                           AncestorType={x:Type DataGrid}}, 
                   Path=DataContext.Countries}" 
              SelectedItem="{Binding Path=Country, 
                   Mode=TwoWay, 
                   UpdateSourceTrigger=PropertyChanged}" /> 
          </Grid> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 


using System.Collections.Generic; 
using System.Windows; 

namespace ComboboxInDataGrid 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      DataContext = new WorldDataViewModel(); 
      InitializeComponent(); 
     } 
    } 

    class WorldDataViewModel 
    { 
     public WorldDataViewModel() 
     { 
      var c1 = "USA"; 
      var c2 = "Canada"; 
      var c3 = "Brasil"; 
      var c4 = "Brasfsdfsil"; 

      Countries = new List<string>(); 
      Countries.Add (c1); 
      Countries.Add (c2); 
      Countries.Add (c3); 
      Countries.Add (c4); 

      _worldDataList.Add (new WorldData { Index = 1, Country = c2 }); 
      _worldDataList.Add (new WorldData { Index = 2, Country = c3 }); 
      _worldDataList.Add (new WorldData { Index = 3, Country = c4 }); 
     } 

     private List<WorldData> _worldDataList = new List<WorldData>(); 
     public List<WorldData> WorldDataList 
     { 
      get 
      { 
       return _worldDataList; 
      } 
      set 
      { 
       _worldDataList = value; 

      } 
     } 

     public List<string> Countries { get; set; } 

     private WorldData worldData; 
     public WorldData SelectedWorldData 
     { 
      get 
      { 
       return worldData; 
      } 

      set 
      { 
       worldData = value; 
      } 
     } 
    } 

    public class WorldData 
    { 
     public int Index { get; set; } 

     private string country; 
     public string Country 
     { 
      get { return country; } 
      set 
      { 
       country = value; 
      } 
     } 
    } 
} 
+0

こんにちは。ちょうどこれを見て。 SelectedCountryにバインドできましたか? SelectedWorldData.Countryにブレークポイントを設定して、国を選択したときに実際にヒットするかどうかを確認しましたか? また、CanUserAddRowsをtrueにする必要があります。あなたの試みをありがとう –

+0

申し訳ありませんが、私は最初の答えを更新しました。今度は 'Country'にブレークを設定すると、デバッガがそこで停止します。 – gomi42

+0

'CanUserAddRows'をtrueに設定できることを忘れてしまいました。新しく作成された行には、UXの観点から少し混乱するかもしれないComboboxが表示されるので、私はそれを無効にしました。再び技術的な制限はありません。 – gomi42

関連する問題