0

Here is my code behind

public partial class ChallanListView : UserControl
{
    public ObservableCollection<ChallanDS> challans = ChallanModel.GetChallansList();
    public ObservableCollection<QualityDS> qualities = ChallanModel.GetQualitiesList();

    public ChallanListView()
    {
        InitializeComponent();
        ChallansDataGrid.DataContext = this;
        ChallansDataGrid.ItemsSource = this.challans;
    }
}

Here is the xaml

<DataGrid Name="ChallansDataGrid" IsReadOnly="True" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Challan Date" Binding="{Binding ChallanDate, StringFormat=d}"/>
        <DataGridTemplateColumn Header="Quality" Binding="?????????????????">
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

For Quality cell I want to bind it to "qualities" list which is in my ChallanListView. How can I do that.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • @mm8 can't we make use of Ancestor feature? I am new to WPF and MVVM. –  Aug 31 '17 at 15:17
  • Definitely not. A DataGridColumn has no visual ancestor to begin with. – mm8 Aug 31 '17 at 15:19
  • 2
    And a column must have a "connection" to a property of the underlying data object for the DataGrid to function as expected. So just forget about this. Add a property to your ChallanDS class and set it on all objects that you intend to display and edit in the DataGrid. – mm8 Aug 31 '17 at 15:24
  • thank you all for your response. –  Aug 31 '17 at 15:28

1 Answers1

0

You can't. You can only bind a column to a property of the data object in the DataGrid's ItemsSource. That's how a DataGrid works.

So you should move whatever property you want to bind to to the ChallanDS class.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    It's not impossible. Just have to work around the visual tree issue. See https://stackoverflow.com/questions/22073740/binding-visibility-for-datagridcolumn-in-wpf – tjmoore May 10 '18 at 16:28