WPF Error: The property Content is set more than once

0 votes
asked Jun 30, 2014 in WPF by Nikhil

I have started learning WPF recently. Please do not mind if this is very silly question but, I do not why I get this error. Below is code I just wrote. I have a ViewModel class called CustomerViewModel with only one property FirstName. So, I am adding reference to that view model and have it as source in my TextBox Binding. But I am getting error as “The property 'Content' is set more than once” and “The object 'Window' already has a child and cannot add 'Grid'.’Window' can accept only one child.” Why do I get this?

<Window x:Class="DataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:DataBinding"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

    <vm:EmployeeVM x:Name="employee" FirstName="Name1"/>

    <Grid>
        <TextBox Text="{Binding FirstName, Source=employee}" HorizontalAlignment="Left" Height="23" Margin="195,58,0,0" 
            TextWrapping="Wrap" VerticalAlignment="Top" Width="239"/>
    </Grid>
</Window>
Share

1 Answer

+1 vote
answered Jun 30, 2014 by Nixon
selected Jun 30, 2014 by administrator
 
Best answer

The above code has couple of issues. First, the containers like Window, Page, UserControl can accept only one child as direct child. That mean, if you want more that one element or items to be added to the Window, then you need to use a containers like Grid, StackPanel, Canvas which can hold more than one child. Since your code has both CustomerViewModel Instance and Grid are added as direct child to Window.

Second issue is, when you want to add Custom CLR object into XAML, you may need to use properties like DataContext or Resources which can hold CLR objects or classes that are derived from Binding / BindingBase.

If you have added your CustomerViewModel under DataContext property of the Window class using Property Element Syntax, you do not even need to use Source Property in TextBox Binding. It will automatically take the source from DataContext, because DataContext is an “Inherited Property” which will be available for all of its children.

<Window x:Class="DataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:DataBinding"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

    <Window.DataContext>
        <vm:EmployeeVM FirstName="Name1"/>
    </Window.DataContext>

    <Grid>
        <TextBox Text="{Binding FirstName}" Height="23" Width="239"/>
    </Grid>
</Window>

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...