13

Problems with data binding when working with WPF

 2 years ago
source link: https://www.codesd.com/item/problems-with-data-binding-when-working-with-wpf.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Problems with data binding when working with WPF

advertisements

I have been trying to learn Visaul C#. Lately, I have been focusing on WPF. Here is the link to the tutorial I have been working on:

http://msdn.microsoft.com/en-us/library/vstudio/ms752299(v=vs.110).aspx

The application should let you view a person's expenses if you select their name and click the View button. Unfortunately, the expenses are not displayed. I keep getting this error message:

System.Windows.Data Error: 50 : XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns="").

I thought that xmlns needed to be left blank, so the app could navigate down the XAML. Here is the code for Grid resource that I made:

<Grid.Resources>
            <!-- Expense Report Data -->
            <XmlDataProvider x:Key="ExpenseDataSource" XPath="Expenses">
                <x:XData>
                    <Expenses xmlns="">
                        <Person Name="Mike" Department="Legal">
                            <Expense ExpenseType="Lunch" ExpenseAmount="50" />
                            <Expense ExpenseType="Transportation" ExpenseAmount="50" />
                        </Person>
                        <Person Name="Lisa" Department="Marketing">
                            <Expense ExpenseType="Document Printing" ExpenseAmount="50" />
                            <Expense ExpenseType="Gift" ExpenseAmount="125" />
                        </Person>
                        <Person Name="John" Department="Engineering">
                            <Expense ExpenseType="Magazine Subscription" ExpenseAmount="50" />
                            <Expense ExpenseType="New Machine" ExpenseAmount="600" />
                            <Expense ExpenseType="Software" ExpenseAmount="500" />
                        </Person>
                        <Person Name="Mary" Department="Finance">
                            <Expense ExpenseType="Dinner" ExpenseAmount="100" />
                        </Person>
                    </Expenses>
                </x:XData>
            </XmlDataProvider>

            <!-- Name item template -->
            <DataTemplate x:Key="nameItemTemplate">
                <Label Content="{Binding XPath=@Name}"/>
            </DataTemplate>

</Grid.Resources>

Can anyone give me any hint about what is going wrong?

Thanks


Turns out the issue was not with the Grid resource! I forgot to set ItemsSource in DataGrid. In my ExpenseReportPage.xaml file, I typed the following:

<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top"
                  HorizontalAlignment="Left">
                <!-- Expense type and Amount table -->
                <DataGrid ItemsSource="{Binding XPath=Expense}" ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
                          AutoGenerateColumns="False" RowHeaderWidth="0">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Expense Type" Binding="{Binding XPath=@ExpenseType}" />
                        <DataGridTextColumn Header="Amount" Binding="{Binding XPath=@ExpenseAmount}"/>
                    </DataGrid.Columns>
                </DataGrid>
</Grid>

After ItemsSource was set, the data was displayed properly.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK