Reputation: 33078
I'm trying to set the ItemsSource
property of a DataGrid
named dgIssueSummary
to be an ObservableCollection
named IssueSummaryList
. Currently, everything is working when I set the ItemsSource
property in my code-behind:
public partial class MainPage : UserControl
{
private ObservableCollection<IssueSummary> IssueSummaryList = new ObservableCollection<IssueSummary>
public MainPage()
{
InitializeComponent();
dgIssueSummary.ItemsSource = IssueSummaryList
}
}
However, I'd rather set the ItemsSource
property in XAML, but I can't get it to work. Here's the XAML code I have:
<sdk:DataGrid x:Name="dgIssueSummary" AutoGenerateColumns="False"
ItemsSource="{Binding IssueSummaryList}" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding ProblemType}" Header="Problem Type"/>
<sdk:DataGridTextColumn Binding="{Binding Count}" Header="Count"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
What do I need to do to set the ItemsSource
property to be the IssueSummaryList
in XAML rather than C#?
Upvotes: 3
Views: 13702
Reputation: 16101
The XAML is correct, so the problem must be in the Binding.
How have you set the Binding? In the most simple case you use code like:
this.DataContext=this;
in the Window_Load eventhandler
Upvotes: 1
Reputation: 5885
Your IssueSummaryList is private. You need to make it a Property with get and set
public ObservableCollection<IssueSummary> IssueSummaryList
{
get
{
// ...
}
}
Upvotes: 1
Reputation: 564363
You need to make "IssueSummaryList" a property. If you do this, you can bind to it directly. You can't bind via Xaml to a private field.
You'll also need to set the DataContext
to "this
" (or use another method to get it to find the appropriate instance).
Upvotes: 3