-2

I have a class in Model.cs and list of the objects of this class is created in viewmodel.cs. I using the list of these objects as anItemsource to the Data Gridview. Each of the object in the list has Name as a property and I want list of these names in the each of the comboxes in the DataGridComboBoxColumn. I tried quite a few things and I'm unable to get the desired result. I'm using Prism for this application

I have the following code in ViewModel.cs

    private IList<BeamData> _allBeams

    public IList<BeamData> AllBeams
    {
        get { return _allBeams; }
        set { _allBeams = value; }
    }


    public LoadInfoViewModel()
    {
         _allBeams = new List<BeamData>()
        {
            new BeamData(){Name ="B1"},
            new BeamData(){Name ="B2"},
            new BeamData(){Name ="B3"}
        };
    }
}

The beam data class looks like this :

    public BeamData()
    {

    }
    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }       

My XAML files has this code

    <DataGrid x:Name="Table" ItemsSource="{Binding AllBeams, UpdateSourceTrigger=Explicit}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridComboBoxColumn ItemsSource="{Binding AllBeams}"
                                DisplayMemberPath="Name">
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

Fyi, This does not answer my question.

Desired output : If you look at the code above, B1, B2, B3 are Name (property) of the object BeamData, each combo boxes in the column should have all the names Viz, B1 , B2, B3

vin
  • 869
  • 4
  • 17
  • 33

1 Answers1

0

Set the ItemsSource in the ElementStyle and EditingElementStyle of the DataGridComboBoxColumn:

<DataGrid x:Name="Table" ItemsSource="{Binding AllBeams, UpdateSourceTrigger=Explicit}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridComboBoxColumn DisplayMemberPath="Name">
            <DataGridComboBoxColumn.ElementStyle>
                <Style TargetType="{x:Type ComboBox}">
                    <Setter Property="ItemsSource" Value="{Binding Path=DataContext.AllBeams, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                </Style>
            </DataGridComboBoxColumn.ElementStyle>
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="{x:Type ComboBox}">
                    <Setter Property="ItemsSource" Value="{Binding Path=DataContext.AllBeams, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

A DataGridComboBoxColumn is not a visual element that inherits the DataContext so you can't directly bind its ItemsSource property to a property of the view model.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • This throws and error : 'DeferRefresh' is not allowed during an AddNew or EditItem transaction – vin Oct 24 '17 at 12:22
  • No, it doesn't. It is something else in your code that throws this exception. – mm8 Oct 24 '17 at 12:24