2017-09-08 9 views
0

DataGrid内のComboBoxにListが設定されていません。 私はItemSourceパスに問題があると思う:ItemSource DataGrid内のComboBoxでのバインド

ビュー(DataGridのXAMLコード):

<DataGrid CanUserAddRows="True" ItemsSource="{Binding Path=GridCollection, Mode=TwoWay}" AutoGenerateColumns="False" IsReadOnly="False"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Column 1"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/> 
        </ComboBox> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
     <DataGridTextColumn Header="Column 2" Width="170" Binding="{Binding Col2, Mode=TwoWay}"/> 
</DataGrid> 

ビューモデルは、(私はItemModelのための監視可能なコレクションを作成し、これが正常に動作しているときに私を)文字列の値を更新した値は、モデルオブジェクトに割り当てられている:

public ObservableCollection<ItemModel> GridCollection 
{ 
    get 
    { 
     return this.gridCollection; 
    } 
    set 
    { 
     this.gridCollection = value; 
     base.RaisedPropertyChanged("GridCollection"); 
    } 
} 

public List<string> ComboBoxList 
{ 
    get 
    { 
     return this.comboBoxList; 
    } 
    set 
    { 
     this.comboBoxList = value; 
     base.RaisedPropertyChanged("GridList"); 
    } 
} 

public MultiValueViewModel(string data) 
{ 
    this.GridCollection = new ObservableCollection<ItemModel>(); 
    this.GridCollection.Add(new ItemModel("ABC", 0)); 
    this.ComboBoxList = new List<string>(); 
    //Add items to list 
} 

モデル(モデル)2つの特性を有する1つのクラスを含んでいる:

public class ItemModel 
{ 
    public ItemModel(string col1, double col2) 
    { 
     this.Col1 = col1; 
     this.Col2 = col2; 
    } 

    public string Col1 { get; set; } 

    public double Col2 { get; set; } 
} 

私はPath = ComboBoxListとDataContext.ComboBoxListで試しましたが、両方とも動作しません。

+0

デバッグ中の 'Output'ウィンドウでのバインディングエラーを確認してください。あなたの目的はゼロのエラーを持つことです。 – Sinatr

答えて

1

はこれを試してください:あなたが親DataGridDataContextのプロパティにバインドする必要がありますので、

<ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList, RelativeSource={RelativeSource AncestorType=DataGrid}}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/> 

ComboBoxDataContextは、デフォルトでItemModelです。上記のように{RelativeSource}を使用してこれを行うことができます。

関連する問題