Omar A. Yousry
Omar A. Yousry

Reputation: 67

Setting DataGrid ItemsSource causes XamlParseException

so I have a problem with an XAML file I'm using; I'm trying to use a DataGrid to add a table view of the properties for an element the user selects. How I'm currently attempting to do this is that I have a list containing the appropriate pairs that gets populated on user click, and then the ItemsSource is set to that list. I have tried changing the details of this implementation (binding the ItemsSource without a reference to the datagrid itself, etc, but sooner or later they all seem to hit the same error)Call Stack Trace when attempting to set ItemsSource

The weird thing (to me) is that after a few clicks on different elements (and hitting 'continue' when the exception pops up) the grid does populate with data, although it often seems to "freeze" (showing the same data for a few elements before finally refreshing a couple of elements later, no exceptions are thrown, but the behaviour is definitely inconsistent)

.xaml.cs

// ParameterPair is a custom class that contains 2 string fields (name, value)
        public List<ParameterPair> AllParameters { get; private set; } = new List<ParameterPair>();


                // called (only) when a new element is click
                // ... code to populate AllParameters here
                // definitely populates properly, checked through debugging
                this.dGrid.ItemsSource = AllParameters;

.xaml

<Page ...>
<Grid HorizontalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <TabControl>
        <TabItem Header="Add Constraint">
            <Grid Name="loginBlock" Grid.Row="0">                        

        <GroupBox Header="Properties"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Top"
                      Margin="10, 10, 10, 0">
                        <StackPanel>


                            <controls:DataGrid x:Name="dGrid" 
                            Height="300" Margin="12"
                            AutoGenerateColumns="true"
                        ItemsSource="{Binding}"
                                               />
                    </StackPanel>
            </GroupBox>
            </Grid>
        </TabItem>
        <TabItem Header="Manage Constraints" />
    </TabControl>

</Grid>
</Page>

Upvotes: 0

Views: 143

Answers (2)

Omar A. Yousry
Omar A. Yousry

Reputation: 67

Okay, so I fixed it. Turns out AutoGenerateColumns="true" was the culprit. I came upon this thread in my frustration, and tried manually setting the columns, which seems to work perfectly fine, just binding the columns to the appropriate fields in the class being used for the list.

                            <DataGrid x:Name="dGrid"
                            Height="300" Margin="12"
                            AutoGenerateColumns="false"
                        ItemsSource="{Binding}"
                                               >
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                                    <DataGridTextColumn Header="Value" Binding="{Binding Value}" />
                                </DataGrid.Columns>
                            </DataGrid>

P.S. I will avoid choosing this as my answer as it is largely a workaround, hopefully someone more experienced will come and give a more thorough and concise solution (maybe myself later on in this project...)

Upvotes: 0

Jan Buijs
Jan Buijs

Reputation: 108

The error doesn't seem to be from your databinding, but from the xaml markup somewhere. Your GroupBox doesn't seem to have closing brackets. And is this a custom DataGrid? Since it's referenced as "controls:DataGrid" unlike your other controls. There might be something wrong in its markup.

Upvotes: 1

Related Questions